{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "be6e1342",
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "import copy\n",
    "import  traceback\n",
    "import shutil\n",
    "import random\n",
    "\n",
    "import numpy as np  # linear algebra\n",
    "import pydicom\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import cv2\n",
    "from pydicom.uid import UID\n",
    "from PIL import Image\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "56a7f6bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_scan(path):\n",
    "    slices = [] #slices = [pydicom.dcmread(path + '/' + s) for s in filter(lambda x: x.endswith('.dcm'), os.listdir(path))]\n",
    "    for s in os.listdir(path):\n",
    "        if os.path.isdir(os.path.join(path, s)): #if not s.endswith('.dcm'):\n",
    "            continue\n",
    "        sl = pydicom.dcmread(os.path.join(path, s))\n",
    "        try:\n",
    "            sl_p = sl.pixel_array\n",
    "        except AttributeError:\n",
    "            traceback.print_exc()\n",
    "            print(f'\\tDelete {os.path.join(path, s)}')\n",
    "            os.remove(os.path.join(path, s))\n",
    "        else:\n",
    "            slices.append(sl)\n",
    "    slices.sort(key=lambda x: float(x.InstanceNumber))\n",
    "    return slices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe13ec63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-100 500\n"
     ]
    }
   ],
   "source": [
    "window_width, window_level = 600, 200\n",
    "lower_b, upper_b = window_level - window_width//2, window_level + window_width//2\n",
    "print(lower_b, upper_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "840077c9",
   "metadata": {},
   "source": [
    "## 1.阳性数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aaf5ad23",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing chenaijun-S-Im25-38-J-Im25-85****\n",
      "****Processing chenazhao-S-Im17-30****\n",
      "****Processing chengshizheng-J-Im30-145****\n",
      "****Processing chenjuli-J-Im18-49****\n",
      "****Processing chenlibo-S-Im39-57****\n",
      "****Processing chenmin-J-Im26-67****\n",
      "****Processing chenqiusheng-J-Im34-143****\n",
      "****Processing chujiancheng-J-Im45-122****\n",
      "****Processing dingzhiqun-S-Im21-29****\n",
      "****Processing fangtianming-S-Im15-30-J-Im15-65****\n",
      "****Processing gaosheng-J-Im19-67****\n",
      "****Processing gaoxiangqin-J-Im24-75****\n",
      "****Processing guanxujun-S-Im25-35-J-Im25-87****\n",
      "****Processing guoheying-J-Im32-127****\n",
      "****Processing hanchun-J-Im20-86****\n",
      "****Processing heguoyu-J-Im36-141****\n",
      "****Processing hexingwen-S-Im25-35-J-Im25-93****\n",
      "****Processing hongjinjun-S-Im24-28-J-Im24-83****\n",
      "****Processing hongzhimin-S-Im30-39-J-30-144****\n",
      "****Processing huajin-J-Im30-67****\n",
      "****Processing huangqiang-J-Im42-147****\n",
      "****Processing huangxiansheng-J-Im25-59****\n",
      "****Processing huangxihong-J-Im27-136****\n",
      "****Processing huangyongliang-J-Im16-81****\n",
      "****Processing huleijun-J-Im25-141****\n",
      "****Processing hupingli-J-Im18-69****\n",
      "****Processing huweibang-S-Im25-30-J-Im25-79****\n",
      "****Processing huxiaomei-S-Im21-24****\n",
      "****Processing jiadeen-J-Im34-148****\n",
      "****Processing jiangboming-J-Im33-40****\n",
      "****Processing jiangdapeng-J-Im29-136****\n",
      "****Processing jiangwangchun-J-Im30-103****\n",
      "****Processing jihongke-S-Im20-24-J-Im20-86****\n",
      "****Processing jinzhangbing-J-Im32-130****\n",
      "****Processing laiguizhen-J-Im38-121****\n",
      "****Processing langhongwei-J-Im24-82****\n",
      "****Processing lianzhaozhen-S-Im23-30-J-Im23-84****\n",
      "****Processing ligang-S-Im33-36****\n",
      "****Processing liuyunfei-J-Im40-95****\n",
      "****Processing liwenjun-J-Im33-132****\n",
      "****Processing liyehua-S-Im30-47-J-Im30-131****\n",
      "****Processing louyidong-S-Im38-58-J-Im38-133****\n",
      "****Processing luguolin-J-Im33-40****\n",
      "****Processing luzhiping-S-Im25-35-J-Im25-54****\n",
      "****Processing lvguanyang-J-Im22-60****\n",
      "****Processing lvrongling-J-Im26-86****\n",
      "****Processing lvxiangshen-J-Im32-139****\n",
      "****Processing maxianfeng-J-Im37-64****\n",
      "****Processing mayongzhong-J-Im22-86****\n",
      "****Processing panguojun-S-Im25-31-J-25-76****\n",
      "****Processing penggaofeng-S-Im28-36****\n",
      "****Processing qianwende-J-Im31-97****\n",
      "****Processing qianyaolin-J-Im36-67****\n",
      "****Processing qiaowei-S-Im18-30-J-Im18-82****\n",
      "****Processing qiuyuanxing-S-Im30-32****\n",
      "****Processing qumin-S-Im22-35-J-Im22-28****\n",
      "****Processing shaoxiulan-J-Im21-25****\n",
      "****Processing shaoyefeng-S-Im34-43-J-Im34-48****\n",
      "****Processing shengwenping-S-Im37-46-J-Im37-116****\n",
      "****Processing shenjie-S-Im39-59-J-Im39-85****\n",
      "****Processing shenqi-J-Im24-72****\n",
      "****Processing shizhongheng-J-Im23-50****\n",
      "****Processing tangabiao-S-Im41-49-J-Im41-138****\n",
      "****Processing tangjinlong-J-Im25-41****\n",
      "****Processing wanglibo-J-Im29-102****\n",
      "****Processing wanglinsen-J-Im38-121****\n",
      "****Processing wangqingdong-J-Im19-88****\n",
      "****Processing wangyuefeng-S-Im34-38-J-Im34-144****\n",
      "****Processing wangziyang-J-Im42-95****\n",
      "****Processing weijuyun-S-Im24-32****\n",
      "****Processing wutonggen-S-Im22-29-J-Im22-82****\n",
      "****Processing xixiaoguo-S-Im16-31-J-Im16-43****\n",
      "****Processing xuguochun-J-Im43-60****\n",
      "****Processing xushichao-J-Im35-145****\n",
      "****Processing xuyuping-S-Im21-33-J-Im21-69****\n",
      "****Processing yangen-J-Im61-183****\n",
      "****Processing yangxiulian-S-Im20-26-J-Im20-44****\n",
      "****Processing yangyuexian-S-Im38-63****\n",
      "****Processing yangzhicheng-J-Im19-82****\n",
      "****Processing yanjuanfeng-J-Im30-32****\n",
      "****Processing yaozaisheng-J-Im16-28****\n",
      "****Processing yesanghua-J-Im41-138****\n",
      "****Processing yexinxiang-S-Im32-38-J-Im32-102****\n",
      "****Processing yexiyou-J-Im35-139****\n",
      "****Processing yingjianquan-S-Im12-14-J-Im12-36****\n",
      "****Processing yujiazhen-S-Im19-27-J-Im19-77****\n",
      "****Processing zhanfangai-S-Im17-23-J-Im17-32****\n",
      "****Processing zhangfuyang-S-Im15-20-J-Im15-87****\n",
      "****Processing zhanghang-S-Im16-20-J-Im16-41****\n",
      "****Processing zhangweiyan-J-Im40-57****\n",
      "****Processing zhangyunqin-S-Im14-25-J-Im14-64****\n",
      "****Processing zhaozanping-J-Im23-90****\n",
      "****Processing zhaozhengxing-J-Im35-99****\n",
      "****Processing zhongweiliang-J-Im66-170****\n",
      "****Processing zhongxuefang-S-Im19-34-J-Im19-72****\n",
      "****Processing zhoubozhong-J-Im29-131****\n",
      "****Processing zhoufeng-S-Im35-40-J-Im35-147****\n",
      "****Processing zhouliang-J-Im47-123****\n",
      "****Processing zhoumingfang-J-Im36-41****\n",
      "****Processing zhuchunguang-J-Im21-86****\n",
      "****Processing zhuhancheng-S-Im27-36-J-Im27-86****\n",
      "****Processing zongminghui-S-Im18-25-J-Im18-81****\n",
      "****Processing baoyin-S-Im9-22-J-Im9-68****\n",
      "****Processing caijilu-J-Im41-87****\n",
      "****Processing caimeiju-J-Im21-81****\n",
      "****Processing caiweiguang-J-Im35-152****\n",
      "****Processing caiying-J-Im34-52****\n",
      "****Processing chenbo-S-Im24-45-J-Im24-97****\n",
      "****Processing chenfujun-J-Im43-100****\n",
      "****Processing chengdazhong-J-Im24-38****\n",
      "****Processing chenggang-J-Im18-81****\n",
      "****Processing chenglongdeng-S-Im21-33-J-Im21-78****\n",
      "****Processing chenguorong-J-Im18-77****\n",
      "****Processing chenjian-J-Im14-81****\n",
      "****Processing chenjinfa-J-Im34-105****\n",
      "****Processing chenjun-J-Im33-137****\n",
      "****Processing chenlili-S-Im18-24-J-Im18-74****\n",
      "****Processing chenmimao-S-Im32-40****\n",
      "****Processing chenping-J-Im23-88****\n",
      "****Processing chenrugu-J-Im21-79****\n",
      "****Processing chensheng-J-Im31-91****\n",
      "****Processing chenweimin-J-Im20-36****\n",
      "****Processing chenwenhua-J-Im41-158****\n",
      "****Processing chenxiaoxiang-J-Im23-88****\n",
      "****Processing chenxuehua-S-Im17-27-J-Im17-35****\n",
      "****Processing chenyicong-J-Im17-58****\n",
      "****Processing chiyanfei-J-Im20-83****\n",
      "****Processing chugentang-J-Im19-80****\n",
      "****Processing daizuokou-J-Im19-74****\n",
      "****Processing dingliyun-J-Im19-57****\n",
      "****Processing dongzhigen-J-Im22-102****\n",
      "****Processing fanghao-J-Im23-89****\n",
      "****Processing fanlanhua-J-Im26-116****\n",
      "****Processing fanxinan-S-Im16-36-J-Im16-30****\n",
      "****Processing gongyongfa-S-Im18-34-J-Im18-87****\n",
      "****Processing guojianfu-J-Im32-100****\n",
      "****Processing guquankang-J-Im18-69****\n",
      "****Processing heyuexiang-J-Im18-76****\n",
      "****Processing hezhangquan-J-Im33-58****\n",
      "****Processing huangdebing-S-Im40-48-J-Im33-138****\n",
      "****Processing huanghai-S-Im19-29-J-Im19-80****\n",
      "****Processing huangsuyue-J-Im21-41****\n",
      "****Processing huangyong-J-Im18-127****\n",
      "****Processing huchaoming-J-Im43-126****\n",
      "****Processing jiangjianchun-J-Im31-151****\n",
      "****Processing jilixian-J-Im34-108****\n",
      "****Processing jinfajun-J-Im22-72****\n",
      "****Processing jiushan-S-Im21-30-J-Im21-82****\n",
      "****Processing laixuexiang-J-Im33-148****\n",
      "****Processing lanshaomei-S-Im20-31-J-Im20-79****\n",
      "****Processing leitufa-S-Im30-60-J-Im30-117****\n",
      "****Processing leixiaoying-S-Im18-26-J-18-71****\n",
      "****Processing liangjianhua-J-Im18-75****\n",
      "****Processing lidaoheng-S-Im22-36-J-Im22-92****\n",
      "****Processing lihuanhuan-S-Im20-28-J-Im20-59****\n",
      "****Processing lijianming-J-Im32-124****\n",
      "****Processing lingenqiang-S-Im30-39****\n",
      "****Processing lingshounv-S-Im18-28-J-Im18-76****\n",
      "****Processing liukesheng-J-Im30-99****\n",
      "****Processing lixueyong-J-Im25-31****\n",
      "****Processing lizhongliang-J-Im19-86****\n",
      "****Processing loulinhua-S-Im27-30****\n",
      "****Processing lufeng-J-Im28-103****\n",
      "****Processing luojun-J-Im22-81****\n",
      "****Processing luoyunshen-J-Im29-94****\n",
      "****Processing lvshibiao-J-Im20-76****\n",
      "****Processing maliwei-S-Im30-48-J-Im30-101****\n",
      "****Processing miqiurao-S-Im22-34--J-Im22-85****\n",
      "****Processing niyongru-S-Im38-43-J-Im38-148****\n",
      "****Processing panbanggeng-J-Im19-78****\n",
      "****Processing panhejian-J-Im39-57****\n",
      "****Processing panzhangsong-J-Im26-91****\n",
      "****Processing pengzhengjiang-J-Im38-160****\n",
      "****Processing qianfuying-J-Im20-87****\n",
      "****Processing qiuzhidong-J-Im25-42****\n",
      "****Processing renjiasheng-J-Im26-88****\n",
      "****Processing ruweiping-S-Im52-63-J-Im52-94****\n",
      "****Processing shenglina-J-Im21-65****\n",
      "****Processing shenliqiang-J-Im29-137****\n",
      "****Processing shenwenjiao-S-Im24-35****\n",
      "****Processing shenxuefu-S-Im33-40-J-Im33-101****\n",
      "****Processing shenyangfeng-J-Im36-120****\n",
      "****Processing shiguohu-J-Im19-70****\n",
      "****Processing shijiuwei-J-Im29-130****\n",
      "****Processing shujianqiang-S-Im22-30-J-Im22-59****\n",
      "****Processing sishouzhong-J-Im25-137****\n",
      "****Processing sizhongyu-J-Im22-81****\n",
      "****Processing sunlibing-J-Im24-82****\n",
      "****Processing sunyudong-J-Im37-95****\n",
      "****Processing tangguoliang-J-Im18-41****\n",
      "****Processing tonghuiling-J-Im22-78****\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing tongjinglin-J-Im36-146****\n",
      "****Processing wangchenrong-S-Im18-24-J-Im18-38****\n",
      "****Processing wangjiangang-J-Im23-60****\n",
      "****Processing wangjiangwei-J-Im37-143****\n",
      "****Processing wangweibo-J-Im36-149****\n",
      "****Processing wangxinchun-J-Im18-81****\n",
      "****Processing wangyaotang-J-Im31-100****\n",
      "****Processing wangyefu-J-Im42-112****\n",
      "****Processing wangyonghui-S-Im18-27-J-Im18-84****\n",
      "****Processing wangzhuxi-J-Im25-144****\n",
      "****Processing weiguanxin-J-Im43-116****\n",
      "****Processing weinaichao-J-Im59-162****\n",
      "****Processing weizhiqing-J-Im33-45****\n",
      "****Processing wenyongguo-J-Im23-121****\n",
      "****Processing wujufen-J-Im31-135****\n",
      "****Processing wuwanglong-J-Im31-125****\n",
      "****Processing wuxiangyang-S-Im18-25****\n",
      "****Processing wuyingbo-J-Im36-98****\n",
      "****Processing wuyueming-J-Im36-138****\n",
      "****Processing xiaminsong-S-Im40-51-J-Im40-139****\n",
      "****Processing xiayunqing-J-Im33-141****\n",
      "****Processing xiechangyou-S-Im22-31-J-Im22-84****\n",
      "****Processing xuanhuili-J-Im30-135****\n",
      "****Processing xuhong-S-Im23-38-J-Im23-40****\n",
      "****Processing xushangfa-J-Im36-141****\n",
      "****Processing xuyanfang-J-Im20-80****\n",
      "****Processing xuyaofeng-J-Im24-119****\n",
      "****Processing yangbaozhi-J-Im27-65****\n",
      "****Processing yangbingshui-S-Im25-33-J-Im25-56****\n",
      "****Processing yangdongshui-S-Im15-21****\n",
      "****Processing yanglanfen-S-Im19-33****\n",
      "****Processing yangmin-J-Im37-150****\n",
      "****Processing yangxiufu-S-Im25-37-J-Im25-29****\n",
      "****Processing yangxuehua-J-Im23-79****\n",
      "****Processing yangyulin-J-Im16-58****\n",
      "****Processing yangzhanxiang-J-Im31-145****\n",
      "****Processing yangzhengfu-S-Im16-30-J-Im16-77****\n",
      "****Processing yaocaiming-J-Im26-88****\n",
      "****Processing yaojianmin-S-Im20-29-J-Im20-86****\n",
      "****Processing yaoliumei-S-Im28-39****\n",
      "****Processing yepeng-J-Im26-125****\n",
      "****Processing yewenyi-J-Im36-130****\n",
      "****Processing yexiyou-S-Im19-27-J-Im19-62****\n",
      "****Processing yingguoliang-J-Im55-130****\n",
      "****Processing yingmeiqi-S-Im22-25-J-Im16-36****\n",
      "****Processing yinshixiu-S-Im23-25-J-Im23-79****\n",
      "****Processing yintianxing-J-Im24-38****\n",
      "****Processing yinyuanyuan-J-Im20-78****\n",
      "****Processing yuanlinyue-J-Im27-120****\n",
      "****Processing yugenrong-J-Im18-60****\n",
      "****Processing yuhongliang-S-Im26-37-J-Im26-105****\n",
      "****Processing yujiada-J-Im21-48****\n",
      "****Processing yujinfang-J-Im31-106****\n",
      "****Processing yulinhang-J-Im14-57****\n",
      "****Processing yuyunguo-J-Im12-15****\n",
      "****Processing zengjun-J-Im22-57****\n",
      "****Processing zhangboqian-S-Im22-32-J-Im22-88****\n",
      "****Processing zhangchaodong-J-Im20-80****\n",
      "****Processing zhangguangming-J-21-138****\n",
      "****Processing zhanghaitao-S-Im25-49-J-Im25-145****\n",
      "****Processing zhanglimin-J-Im40-149****\n",
      "****Processing zhangshenxiang-J-Im41-83****\n",
      "****Processing zhangshiliang-J-Im32-49****\n",
      "****Processing zhangyide-J-Im28-107****\n",
      "****Processing zhangying-S-Im29-44****\n",
      "****Processing zhangzhefang-S-Im22-31-J-Im22-59****\n",
      "****Processing zhaodaqing-J-Im12-68****\n",
      "****Processing zhaowenxian-J-Im43-111****\n",
      "****Processing zhengjiyou-J-Im28-136****\n",
      "****Processing zhenhui-J-Im28-101****\n",
      "****Processing zhoufengnian-J-Im24-121****\n",
      "****Processing zhouguoyang-J-Im31-96****\n",
      "****Processing zhourongjie-S-Im19-27-J-Im19-64****\n",
      "****Processing zhousuhua-J-Im25-96****\n",
      "****Processing zhouyingnan-J-Im47-125****\n",
      "****Processing zhujunfei-J-Im24-88****\n",
      "****Processing zhuseng-S-Im24-31-J-Im24-105****\n",
      "****Processing zhuxutao-J-Im23-78****\n",
      "****Processing zhuyongfu-J-Im22-80****\n",
      "****Processing zhuyuejin-J-Im21-72****\n",
      "****Processing baozuoshi-J-16-79****\n",
      "****Processing changzhenxuan-J-15-55****\n",
      "****Processing chenaijiao-J-26-41****\n",
      "****Processing chencaiying-J-19-79****\n",
      "****Processing chenfuliang-S-21-31-J-21-84****\n",
      "****Processing chenganyao-J-19-40****\n",
      "****Processing chenhui-S-17-25-J-17-87****\n",
      "****Processing chenjufa-J-16-66****\n",
      "****Processing digenlan-J-23-50****\n",
      "****Processing fangchunfeng-J-16-48****\n",
      "****Processing fangzhuxiang-J-18-61****\n",
      "****Processing fudehe-S-20-33-J-20-70****\n",
      "****Processing ganxiaobin-J-23-51****\n",
      "****Processing huangwanman-S-15-30****\n",
      "****Processing kongxiaoqing-S-19-32-J-27-94****\n",
      "****Processing liangjianjun-S-17-23****\n",
      "****Processing linfaqiu-S-18-29****\n",
      "****Processing linjiaxiang-J-21-72****\n",
      "****Processing linrichun-J-16-72****\n",
      "****Processing lirucou-J-19-59****\n",
      "****Processing liubihai-S-23-26-J-23-93****\n",
      "****Processing liuhongfu-J-57-200****\n",
      "liuhongfu-J-57-200 height not equal to width\n",
      "\n",
      "****Processing liuzhangliu-J-22-28****\n",
      "****Processing nixueya-J-19-75****\n",
      "****Processing pancunxiao-S-16-30-J-16-70****\n",
      "****Processing shanghongjun-J-22-84****\n",
      "****Processing shenjian-J-15-81****\n",
      "****Processing siyoulin-S-16-26****\n",
      "****Processing sunhongliang-S-23-34****\n",
      "****Processing wangjicheng-J-14-22****\n",
      "****Processing wangxiaofu-S-19-29-J-19-65****\n",
      "****Processing xiafangzhou-S-18-25-J-18-80****\n",
      "****Processing xiangweiwen-J-25-52****\n",
      "****Processing xucaixiang-J-17-36****\n",
      "****Processing xuhangying-J-25-63****\n",
      "****Processing xuheping-S-19-35-J-19-70****\n",
      "****Processing xujiahua-S-20-33-J-20-71****\n",
      "****Processing xupinglun-S-18-28-J-18-32****\n",
      "****Processing yejiangen-J-15-34****\n",
      "****Processing yuguiying-S-16-22-J-16-77****\n",
      "****Processing yujunying-J-15-82****\n",
      "****Processing zhangjiahua-J-19-78****\n",
      "****Processing zhangjianliang-J-16-77****\n",
      "****Processing zhangping-J-25-88****\n",
      "****Processing zhengchong-S-16-31-J-16-87****\n",
      "****Processing zhongchuankun-J-17-69****\n",
      "****Processing zhoujianglong-S-17-31****\n",
      "****Processing zhuxinjian-J-20-23****\n",
      "****Processing baochunsheng-J-20-82****\n",
      "****Processing baojinan-J-24-87****\n",
      "****Processing chengyonghui-J-17-81****\n",
      "****Processing chenjiandong-S-19-28-J-19-91****\n",
      "****Processing chensiqi-S-17-28-J-17-69****\n",
      "****Processing dukun-S-20-22-J-20-23****\n",
      "****Processing fanghongwei-J-22-71****\n",
      "****Processing fangyuxian-J-23-60****\n",
      "****Processing guoyinhua-J-13-87****\n",
      "****Processing heguangfa-J-27-88****\n",
      "****Processing huanglijun-J-24-98****\n",
      "****Processing jinchongfei-J-22-92****\n",
      "****Processing liguangjun-S-25-31-J-25-93****\n",
      "****Processing liguirong-S-18-22****\n",
      "****Processing linkegui-S-18-33-J-18-54****\n",
      "****Processing lisiquan-S-68-70-J-68-89****\n",
      "****Processing liubingsong-J-20-84****\n",
      "****Processing liudongxian-J-16-66****\n",
      "****Processing liushufen-S-18-27-J-18-78****\n",
      "****Processing luohongzhang-J-17-41****\n",
      "****Processing panhualin-S-24-29****\n",
      "****Processing pengshihong-J-23-53****\n",
      "****Processing qiansongbo-J-19-24****\n",
      "****Processing shanlegen-S-22-30-J-22-79****\n",
      "****Processing shiaihong-J-36-80****\n",
      "****Processing sunhongbin-J-19-66****\n",
      "****Processing tanjiarong-S-17-23****\n",
      "****Processing tongyunbao-J-18-69****\n",
      "****Processing wangliqiang-J-20-86****\n",
      "****Processing wangqing-J-35-102****\n",
      "****Processing wangqiyuan-J-16-62****\n",
      "****Processing wangzhengjin-J-16-82****\n",
      "****Processing xiashubiao-J-24-88****\n",
      "****Processing xingxiangjuan-J-20-76****\n",
      "****Processing xumeijuan-J-16-66****\n",
      "****Processing xuweiliang-S-22-25-J-22-74****\n",
      "****Processing xuxingzheng-J-16-63****\n",
      "****Processing xuyongbiao-J-18-67****\n",
      "****Processing xuzhaofang-S-21-26-J-21-86****\n",
      "****Processing yangmingjun-S-15-24-J-15-61****\n",
      "****Processing yaowenhao-J-36-47****\n",
      "****Processing yexianchang-S-17-30-J-17-74****\n",
      "****Processing yuguiji-S-31-33****\n",
      "****Processing yuguoping-J-19-35****\n",
      "****Processing zhangjian-J-26-89****\n",
      "****Processing zhangmaoxiang-S-25-34****\n",
      "****Processing zhangshiqin-J-15-22****\n",
      "****Processing zhangwei-S-20-26-J-20-61****\n",
      "****Processing zhangwenzhong-J-16-68****\n",
      "****Processing zhangxueqiao-S-26-31****\n",
      "****Processing zhengguozheng-J-18-81****\n",
      "****Processing zhouweigui-J-21-80****\n",
      "****Processing chencaixi-J-16-53****\n",
      "****Processing chenchanglu-J-142-168****\n",
      "****Processing chengchunxiao-S-39-57-J-39-93****\n",
      "****Processing chenyiqing-J-69-117****\n",
      "****Processing chenyouli-J-34-93****\n",
      "****Processing chenzhiqing-J-19-55****\n",
      "****Processing dengxiuzhu-S-27-47-J-27-86****\n",
      "****Processing fangmengzhen-J-20-73****\n",
      "****Processing guanzaihua-J-25-61****\n",
      "****Processing heazi-S-16-30****\n",
      "****Processing hezhengmin-J-24-74****\n",
      "****Processing hongshunchang-J-26-89****\n",
      "****Processing huangbaichao-J-31-105****\n",
      "****Processing hucailu-J-32-55****\n",
      "****Processing huyaxiang-J-40-89****\n",
      "****Processing huyingfu-S-31-39-J-31-105****\n",
      "****Processing jiangbihua-S-21-30-J-21-55****\n",
      "****Processing jiangjianping-J-17-63****\n",
      "****Processing jiangqianfeng-J-35-98****\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing jiatiandeng-S-41-55-J-41-142****\n",
      "****Processing jinguoqing-S-15-24-J-15-76****\n",
      "****Processing kebaoming-S-29-40****\n",
      "****Processing laidaishan-J-24-47****\n",
      "****Processing lichengqian-J-35-100****\n",
      "****Processing liguihua-S-22-24****\n",
      "****Processing limingyu-J-27-55****\n",
      "****Processing lindepei-J-136-158****\n",
      "****Processing lingsulan-J-46-107****\n",
      "****Processing linjian-J-34-105****\n",
      "****Processing lisanliang-J-37-101****\n",
      "****Processing lisong-J-39-158****\n",
      "****Processing luae-J-30-83****\n",
      "****Processing lujikang-S-17-31****\n",
      "****Processing luojincha-S-12-24-J-12-44****\n",
      "****Processing miaoyihua-J-17-28****\n",
      "****Processing nimeihua-S-27-43-J-27-64****\n",
      "****Processing ouyangyuedong-S-23-28-J-23-58****\n",
      "****Processing panchenglei-S-29-36-J-29-78****\n",
      "****Processing panxiaohua-S-23-30-J-23-58****\n",
      "****Processing pengxuechuan-S-34-49-J-34-110****\n",
      "****Processing qianxiaofu-J-13-68****\n",
      "****Processing qichunhong-J-34-37****\n",
      "****Processing qiuhongren-J-41-68****\n",
      "****Processing shenchunyan-S-43-54-J-43-148****\n",
      "****Processing shenjianyi-J-31-76****\n",
      "****Processing shenlinhua-S-35-43-J-35-147****\n",
      "****Processing shenqingchuan-S-26-39****\n",
      "****Processing shenyang-J-34-96****\n",
      "****Processing shizheng-J-32-101****\n",
      "****Processing songshuizhen-J-9-43****\n",
      "****Processing songxin-J-23-98****\n",
      "****Processing sunhuhua-J-31-103****\n",
      "****Processing sunshaozhi-J-20-64****\n",
      "****Processing sunshimin-J-22-45****\n",
      "****Processing taolidi-J-41-121****\n",
      "****Processing tianjianjun-S-40-65-J-40-125****\n",
      "****Processing wangchunli-S-19-32****\n",
      "****Processing wangjinlong-S-33-43-J-33-76****\n",
      "****Processing wangruixing-J-25-63****\n",
      "****Processing wangwenzhong-J-32-122****\n",
      "****Processing wanlaoge-J-40-81****\n",
      "****Processing wuabao-J-20-52****\n",
      "****Processing wuguomiao-J-22-50****\n",
      "****Processing wurenjin-J-29-103****\n",
      "****Processing wurihua-J-30-64****\n",
      "****Processing wuxisheng-S-27-31-J-27-27****\n",
      "****Processing wuzonghe-J-15-62****\n",
      "****Processing xialiming-J-22-66****\n",
      "****Processing xiashengyun-J-20-79****\n",
      "****Processing xigenmu-J-22-58****\n",
      "****Processing xiongliandi-S-19-33-J-19-59****\n",
      "****Processing xuaijun-J-34-91****\n",
      "****Processing xujufeng-S-17-27-J-17-35****\n",
      "****Processing xuliang-J-34-142****\n",
      "****Processing xulinfeng-S-15-29-J-15-39****\n",
      "****Processing xuqindi-S-29-39-J-29-71****\n",
      "****Processing xuyuezhen-J-38-108****\n",
      "****Processing xuzhengcheng-J-29-58****\n",
      "****Processing yangyanwen-J-22-40****\n",
      "****Processing yanyueying-J-30-98****\n",
      "****Processing yanzhengmin-J-36-103****\n",
      "****Processing yeguixiang-S-37-49-J-37-100****\n",
      "****Processing yulitang-S-24-40****\n",
      "****Processing zhangdeting-J-20-56****\n",
      "****Processing zhangdongju-J-26-33****\n",
      "****Processing zhangjufen-S-20-41-J-20-54****\n",
      "****Processing zhanglingyun-J-17-57****\n",
      "****Processing zhanglvhao-J-24-41****\n",
      "****Processing zhangrijun-J-23-66****\n",
      "****Processing zhangrufen-S-25-38-J-25-77****\n",
      "****Processing zhangweihu-J-26-38****\n",
      "****Processing zhangxiaoshun-S-24-35-J-24-90****\n",
      "****Processing zhangyougou-J-11-73****\n",
      "****Processing zhaoxiao-S-20-30-J-20-48****\n",
      "****Processing zhengxinfu-J-36-74****\n",
      "****Processing zhengyingchao-J-55-77****\n",
      "****Processing zhongafen-J-17-55****\n",
      "****Processing zhongmiaoxian-S-16-26-J-16-34****\n",
      "****Processing zhoulongxing-J-15-53****\n",
      "****Processing zhouquantai-J-41-78****\n",
      "****Processing zhuxuequn-J-97-104****\n",
      "****Processing zhuyucai-S-19-29****\n",
      "****Processing bairuixin-J-29-46****\n",
      "****Processing baochangmu-J-18-77****\n",
      "****Processing baodezhong-J-41-44****\n",
      "****Processing chenbingrong-J-61-96****\n",
      "****Processing chenfuding-J-29-37****\n",
      "****Processing chenmazhang-J-18-22****\n",
      "****Processing chenmingbao-J-38-96****\n",
      "****Processing chenrongqin-J-32-100****\n",
      "****Processing chensimeng-J-31-33****\n",
      "****Processing dongyoufa-J-48-87****\n",
      "****Processing fanggaoshen-J-82-87****\n",
      "****Processing fangpingan-J-29-34****\n",
      "****Processing feiliangming-J-23-41****\n",
      "****Processing gaohuarong-J-51-109****\n",
      "****Processing gaozhihua-J-82-94****\n",
      "****Processing gebingzhao-J-38-40****\n",
      "****Processing guxin-J-80-88****\n",
      "****Processing heqiang-J-36-45****\n",
      "****Processing heshimo-J-44-46****\n",
      "****Processing heshufang-J-28-30****\n",
      "****Processing huajinyuan-J-58-82****\n",
      "****Processing huanggentian-J-121-132****\n",
      "****Processing huangyuanzhong-J-24-103****\n",
      "****Processing huboan-J-33-131****\n",
      "****Processing huxuegen-S-37-37-J-60-74****\n",
      "****Processing jiangxianghong-J-132-146****\n",
      "****Processing jinguozhong-J-37-116****\n",
      "****Processing lifurong-J-54-62****\n",
      "****Processing linjinmu-J-36-120****\n",
      "****Processing linxiulian-S-17-18-J-17-17****\n",
      "****Processing linyunfu-S-27-29****\n",
      "****Processing liushurong-J-72-78****\n",
      "****Processing liuyongliang-J-122-131****\n",
      "****Processing liyi-J-23-42****\n",
      "****Processing liyonghong-J-48-54****\n",
      "****Processing liyuezhi-J-53-60****\n",
      "****Processing lizuojing-J-35-38****\n",
      "****Processing magengqiu-S-50-55****\n",
      "****Processing maoshangxin-J-44-81****\n",
      "****Processing meichunming-J-116-119****\n",
      "****Processing miaohengtai-S-36-39-J-36-135****\n",
      "****Processing nigengrong-J-25-32****\n",
      "****Processing qiaozhiyin-J-55-65-J-117-142****\n",
      "****Processing qiuguozhang-J-68-77****\n",
      "****Processing qiujinrong-J-33-57-J-78-125-J-137-158****\n",
      "****Processing qiumeijun-J-45-52****\n",
      "****Processing quanjianzhong-S-32-35****\n",
      "****Processing rongjiliang-J-31-43****\n",
      "****Processing shengyueqin-J-21-41****\n",
      "****Processing songguoan-J-30-40****\n",
      "****Processing suzhijing-J-51-55****\n",
      "****Processing tuchenghe-J-90-123****\n",
      "****Processing tuhuadong-J-22-30****\n",
      "****Processing wangbaosheng-J-57-87****\n",
      "****Processing wangdeqian-J-36-123****\n",
      "****Processing wangdequan-J-25-121****\n",
      "****Processing wangguoping-J-31-94****\n",
      "****Processing wangshiyao-J-46-139****\n",
      "****Processing wangyeping-J-69-78****\n",
      "****Processing wugenbao-J-16-78****\n",
      "****Processing xiahongsheng-J-24-86****\n",
      "****Processing xiangbaiquan-J-38-100****\n",
      "****Processing xingwenqu-J-24-53****\n",
      "****Processing xuping-J-35-79****\n",
      "****Processing xuwenxiang-J-50-67****\n",
      "****Processing yingguocheng-J-48-54****\n",
      "****Processing yujianshun-S-19-20****\n",
      "****Processing yuxingguan-J-25-30****\n",
      "****Processing zengchenhong-J-80-86****\n",
      "****Processing zhangshaohong-S-17-20****\n",
      "****Processing zhengaizhu-S-26-29-J-52-56****\n",
      "****Processing zhengxiusheng-S-26-27****\n",
      "****Processing zhongjianhong-J-29-63****\n",
      "****Processing zhongzhongnan-J-22-37****\n",
      "****Processing zhougensheng-S-39-42****\n",
      "****Processing zhourongcheng-J-80-93****\n",
      "****Processing zhouxincang-J-40-97****\n",
      "****Processing zhujianping-J-31-34****\n",
      "****Processing baoguizhi-J-111-138****\n",
      "****Processing caicunyuan-S-23-26****\n",
      "****Processing changyifan-S-72-80****\n",
      "****Processing chenfengxian-S-32-43****\n",
      "****Processing chengrenhua-S-35-45****\n",
      "****Processing chenguoming-S-57-67****\n",
      "****Processing chenjufang-S-31-42****\n",
      "****Processing chenjunliang-S-46-58****\n",
      "****Processing chenlingdi-J-103-127****\n",
      "****Processing chenweiming-J-88-99****\n",
      "****Processing chenyegen-S-54-59****\n",
      "****Processing chuxingnan-S-30-34****\n",
      "****Processing cuiruxing-S-47-54****\n",
      "****Processing dingbilian-S-44-57****\n",
      "****Processing dujiang-S-33-37****\n",
      "****Processing fanghonghong-J-128-149****\n",
      "****Processing fubaoheng-S-24-35****\n",
      "****Processing fuyingliang-S-48-62****\n",
      "****Processing gancaiping-S-43-48****\n",
      "****Processing guoguican-J-75-77****\n",
      "****Processing guronghua-S-30-33****\n",
      "****Processing gushiheng-J-47-68****\n",
      "****Processing guxinmei-S-27-33****\n",
      "****Processing helvtang-J-129-140****\n",
      "****Processing heziquan-J-124-133****\n",
      "****Processing hongshuirong-S-28-42****\n",
      "****Processing huangfujuan-S-61-68****\n",
      "****Processing huangxiaogen-J-100-118****\n",
      "****Processing hujiangxiang-S-53-58****\n",
      "****Processing jiangkaishui-S-25-27****\n",
      "****Processing jiangxiaowei-J-37-40****\n",
      "****Processing jiangyaling-S-29-42****\n",
      "****Processing jinline-S-19-26****\n",
      "****Processing kangzhenzhong-S-33-38****\n",
      "****Processing kongxiangbao-S-59-69****\n",
      "****Processing kongxiuchun-S-57-71****\n",
      "****Processing laibaozhu-J-22-28****\n",
      "****Processing laiyong-J-31-47****\n",
      "****Processing leihexiang-J-24-27****\n",
      "****Processing liujun-J-46-61****\n",
      "****Processing louaizhen-S-20-30****\n",
      "****Processing lubihua-S-27-46****\n",
      "****Processing lugenqian-S-41-59****\n",
      "****Processing luguilin-S-36-49****\n",
      "****Processing luoxianwu-J-43-63****\n",
      "****Processing lvhanfang-S-34-40****\n",
      "****Processing maomao-J-45-51****\n",
      "****Processing nishengwei-S-64-74****\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing panguangwen-S-24-29****\n",
      "****Processing panxianghua-J-40-46****\n",
      "****Processing qianqiliang-J-43-49****\n",
      "****Processing shanmingdong-S-49-53****\n",
      "****Processing shaofeng-S-29-37****\n",
      "****Processing shenfabiao-S-43-60****\n",
      "****Processing shenhuinian-S-28-33****\n",
      "****Processing shenshuigen-S-43-56****\n",
      "****Processing shensongping-J-48-67****\n",
      "****Processing shentuduanyang-S-44-55****\n",
      "****Processing tongyanwen-S-31-41****\n",
      "****Processing wangchengshe-S-44-48****\n",
      "****Processing wanghongchu-S-48-66****\n",
      "****Processing wangshuilin-S-51-58****\n",
      "****Processing wangxiaozhong-S-18-28****\n",
      "****Processing wangxiue-J-26-96****\n",
      "****Processing wangyouxing-S-59-67****\n",
      "****Processing weijilin-J-76-91****\n",
      "****Processing weiwanbo-S-24-41****\n",
      "****Processing wupingzhen-S-20-27****\n",
      "****Processing wuquanxin-J-117-124****\n",
      "****Processing wusunqing-S-27-32****\n",
      "****Processing wuyuanfu-J-22-25****\n",
      "****Processing wuzhongqiu-S-39-56****\n",
      "****Processing xiaofuxing-S-36-51****\n",
      "****Processing xucaifeng-S-39-43****\n",
      "****Processing xuchanghua-J-125-131****\n",
      "****Processing xufu-S-35-53****\n",
      "****Processing xujianhua-S-22-31****\n",
      "****Processing xuke-J-110-120****\n",
      "****Processing yangjinxian-S-34-46****\n",
      "****Processing yangkanghe-S-43-61****\n",
      "****Processing yangyunxian-J-103-110****\n",
      "****Processing yaoae-S-42-60****\n",
      "****Processing yechunju-J-29-38****\n",
      "****Processing yeyunhai-J-142-147****\n",
      "****Processing yuguobin-S-27-59****\n",
      "****Processing yumeizhen-S-38-51****\n",
      "****Processing zhangbaoyou-S-46-59****\n",
      "****Processing zhangjun-J-70-82****\n",
      "****Processing zhanglifang-J-106-114****\n",
      "****Processing zhanglugen-S-45-47****\n",
      "****Processing zhangmeijuan-J-15-19****\n",
      "****Processing zhangsengao-S-21-27****\n",
      "****Processing zhangshenhong-J-36-48****\n",
      "****Processing zhangweiyong-S-49-55****\n",
      "****Processing zhangyanjun-S-31-35****\n",
      "****Processing zhangyinhai-J-72-80****\n",
      "****Processing zhaofaqin-S-39-53****\n",
      "****Processing zhengxihong-S-22-29****\n",
      "****Processing zhoujuqin-S-41-62****\n",
      "****Processing zhoushuimu-J-112-115****\n",
      "****Processing zhoutucai-J-43-50-S-69-74****\n",
      "****Processing zhouwudi-J-43-49****\n",
      "****Processing zhucaiqin-S-32-49****\n",
      "****Processing zhuxingfu-S-21-31****\n",
      "****Processing zhuzhiying-S-22-37****\n",
      "****Processing 俞沧子-S-82-89-J-82-258****\n",
      "****Processing 傅阿乔-S-69-115-J-96-257****\n",
      "****Processing 卢承流-J-79-350****\n",
      "****Processing 叶超涵-S-97-103-J-97-323****\n",
      "****Processing 姜浩芳-S-63-118-J-63-288****\n",
      "****Processing 孙建华-J-64-173****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/charset.py:703: UserWarning: Value 'GB18030' cannot be used as code extension, ignoring it\n",
      "  py_encodings = _handle_illegal_standalone_encodings(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing 张林生-J-75-237****\n",
      "****Processing 朱大波-J-23-179****\n",
      "****Processing 李资浩-S-41-68****\n",
      "****Processing 欧兆辉-J-59-245****\n",
      "****Processing 江西向-J-114-274****\n",
      "****Processing 蒋世良-S-47-101-J-47-312****\n",
      "****Processing 许建敏-J-102-397****\n",
      "****Processing 许杏琴-J-150-266****\n",
      "****Processing 赵晴-J-9-75****\n",
      "****Processing 陈志武-J-69-407****\n",
      "****Processing 韩桂英-J-92-143****\n",
      "****Processing 马康美-S-74-114-J-74-318****\n",
      "****Processing 俞子珊-J-71-191****\n",
      "****Processing 刘培华-J-56-224****\n",
      "****Processing 姚群芳-J-70-141-J-160-192****\n",
      "****Processing 孙业武-S-100-164-J-100-306****\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zsxm/miniconda3/envs/pytorch/lib/python3.8/site-packages/pydicom/charset.py:703: UserWarning: Value 'ISO_IR 192' for Specific Character Set does not allow code extensions, ignoring: GB18030\n",
      "  py_encodings = _handle_illegal_standalone_encodings(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing 张信基-J-54-62****\n",
      "****Processing 慈能满-J-76-106****\n",
      "****Processing 章国林-J-82-180****\n",
      "****Processing 章群娣-S-59-81-J-59-73****\n",
      "****Processing 陈金财-J-69-226****\n",
      "****Processing 亓富兰-J-14-78****\n",
      "****Processing 冷清裕-J-186-328****\n",
      "****Processing 刘世杰-J-212-270****\n",
      "****Processing 刘炳付-J-329-360****\n",
      "****Processing 吕正雄-J-286-293****\n",
      "****Processing 吴金荣-J-277-282****\n",
      "****Processing 周朝进-J-264-288****\n",
      "****Processing 周舍福-J-74-82****\n",
      "****Processing 唐秋平-J-535-542-J-550-566****\n",
      "****Processing 徐行福-J-387-396****\n",
      "****Processing 戴照圣-J-116-255****\n",
      "****Processing 戴行铨-J-275-309****\n",
      "****Processing 曹建花-J-284-290****\n",
      "****Processing 曹生玉-J-54-237****\n",
      "****Processing 朱惠贤-J-85-98****\n",
      "****Processing 李佩芝-J-101-216****\n",
      "****Processing 李永琪-J-504-513****\n",
      "****Processing 杜广胜-J-186-370****\n",
      "****Processing 武汉卿-J-314-324****\n",
      "****Processing 潘根朝-J-114-324****\n",
      "****Processing 童利君-J-21-24****\n",
      "****Processing 茹志莲-J-78-253****\n",
      "****Processing 谢必廉-J-68-76****\n",
      "****Processing 谢文龙-J-72-79****\n",
      "****Processing 金有才-J-93-110****\n",
      "****Processing 钟清廉-J-247-252****\n",
      "****Processing 陈胜平-J-60-262****\n"
     ]
    }
   ],
   "source": [
    "# 将dcm文件根据窗宽窗位转化为png图片\n",
    "def generate_image(input_folder):\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        for scan in os.listdir(os.path.join(input_folder, patient)):\n",
    "            if scan != '1':\n",
    "                continue\n",
    "            name = patient #name = patient.split('-')[0]\n",
    "            image_path = os.path.join(input_folder, patient, scan, f'images_{lower_b}_{upper_b}')\n",
    "            if os.path.exists(image_path):\n",
    "                shutil.rmtree(image_path)\n",
    "            os.mkdir(image_path)\n",
    "\n",
    "            ct = load_scan(os.path.join(input_folder, patient, scan))\n",
    "            print_flag = False\n",
    "            for i in range(len(ct)):\n",
    "                img = ct[i].pixel_array.astype(np.int16)\n",
    "                intercept = ct[i].RescaleIntercept\n",
    "                slope = ct[i].RescaleSlope\n",
    "                if slope != 1:\n",
    "                    img = (slope * img.astype(np.float64)).astype(np.int16)\n",
    "                img += np.int16(intercept)\n",
    "                img = np.clip(img, lower_b, upper_b)\n",
    "                img = ((img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "                img = Image.fromarray(img)\n",
    "                if img.height != img.width and not print_flag:\n",
    "                    print(patient, 'height not equal to width\\n')\n",
    "                    print_flag = True\n",
    "                img.save(os.path.join(image_path, f'{name}_{i:04d}.png'))\n",
    "\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-8/')\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-13/')\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-19/')\n",
    "generate_image('/nfs3-p1/zsxm/dataset/2021-9-28/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-10-19-imh/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-10-19-pau/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-10-19-aa/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20-imh/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-11-20-pau/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdc7d988",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将各个病例中的png图片文件夹统一移动到一起供yolov5检测\n",
    "def move_together_for_detect(input_folder, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    root_name = input_folder.split('/')[-1] if input_folder.split('/')[-1] != '' else input_folder.split('/')[-2]\n",
    "    dst_path = os.path.join(dst_path, root_name)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        if os.path.exists(os.path.join(dst_path, name)):\n",
    "            print(f\"\\tremove {os.path.join(dst_path, name)}\")\n",
    "            shutil.rmtree(os.path.join(dst_path, name))\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '1', f'images_{lower_b}_{upper_b}'), os.path.join(dst_path, name))\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-8/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-13/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-19/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-9-28/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-10-19-imh/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "# move_together_for_detect('/nfs3-p1/zsxm/dataset/2021-10-19-pau/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p2/zsxm/dataset/2021-11-20/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p2/zsxm/dataset/2021-11-20-imh/', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p2/zsxm/dataset/2021-11-20-pau/', '/nfs3-p1/zsxm/dataset/9_detect/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ff58dc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-8', '/nfs3-p1/zsxm/dataset/9_detect/pred')\n",
    "# shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-13', '/nfs3-p1/zsxm/dataset/9_detect/pred')\n",
    "# shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-19', '/nfs3-p1/zsxm/dataset/9_detect/pred')\n",
    "# shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-28', '/nfs3-p1/zsxm/dataset/9_detect/pred')\n",
    "shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/aorta_temp', '/nfs3-p1/zsxm/dataset/9_detect/pred')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07a41e2b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 用在更换窗宽窗位的时候，因为已经经过yolov5的检测生成了标签，故直接移动图片到images文件夹下即可\n",
    "def directly_move_to_image_label(input_folder, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        image_path = os.path.join(dst_path, name, 'images')\n",
    "        if os.path.exists(image_path):\n",
    "            print(f\"\\tremove {image_path}\")\n",
    "            shutil.rmtree(image_path)\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '1', f'images_{lower_b}_{upper_b}'), image_path)\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "            \n",
    "# directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-8/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-13/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-19/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-28/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# directly_move_to_image_label('/nfs3-p2/zsxm/dataset/2021-10-19-imh/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh')\n",
    "# directly_move_to_image_label('/nfs3-p2/zsxm/dataset/2021-10-19-pau/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau')\n",
    "directly_move_to_image_label('/nfs3-p2/zsxm/dataset/2021-11-20/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2')\n",
    "directly_move_to_image_label('/nfs3-p2/zsxm/dataset/2021-11-20-imh/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2')\n",
    "directly_move_to_image_label('/nfs3-p2/zsxm/dataset/2021-11-20-pau/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22933554",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将移动到待测文件夹中的原图移动到images文件夹\n",
    "def move_ori_image_to_sigle_folder(input_path, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    for patient in os.listdir(input_path):\n",
    "        print(f'****Processing {patient}****')\n",
    "        if not os.path.exists(os.path.join(dst_path, patient)):\n",
    "            os.mkdir(os.path.join(dst_path, patient))\n",
    "        image_path = os.path.join(dst_path, patient, 'images')\n",
    "        if os.path.exists(image_path):\n",
    "            shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for img in os.listdir(os.path.join(input_path, patient)):\n",
    "            if os.path.isfile(os.path.join(input_path, patient, img)):\n",
    "                shutil.move(os.path.join(input_path, patient, img), os.path.join(image_path, img))\n",
    "        os.rmdir(os.path.join(input_path, patient))\n",
    "    os.rmdir(input_path)\n",
    "    \n",
    "# move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-8/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-13/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-19/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-28/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-11-20/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2')\n",
    "move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-11-20-imh/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2')\n",
    "move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-11-20-pau/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f5b969c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将yolov5的检测结果（标签和带bounding box的图像）移动到相应文件夹\n",
    "def move_detect_result_to_folder(input_path, output_path):\n",
    "    for patient in os.listdir(input_path):\n",
    "        print(f'Processing {patient}')\n",
    "        image_path = os.path.join(output_path, patient, 'pred_images')\n",
    "        if os.path.exists(image_path):\n",
    "            shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for file in os.listdir(os.path.join(input_path, patient)):\n",
    "            if os.path.isfile(os.path.join(input_path, patient, file)):\n",
    "                shutil.move(os.path.join(input_path, patient, file), os.path.join(image_path, file))\n",
    "            elif os.path.isdir(os.path.join(input_path, patient, file)):\n",
    "                if os.path.exists(os.path.join(output_path, patient, file)):\n",
    "                    shutil.rmtree(os.path.join(output_path, patient, file))\n",
    "                shutil.move(os.path.join(input_path, patient, file), os.path.join(output_path, patient, file))\n",
    "        os.rmdir(os.path.join(input_path, patient))\n",
    "    os.rmdir(input_path)\n",
    "                \n",
    "# move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-8/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-13/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-19/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "# move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-28/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive')\n",
    "move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-11-20/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2')\n",
    "move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-11-20-imh/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2')\n",
    "move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-11-20-pau/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a10cf95",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 切出主动脉\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.2 < y < 0.8, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.2 < corr1[2] < 0.8, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.2 < corr2[2] < 0.8, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, 'images')\n",
    "        label_path = os.path.join(input_path, patient, 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, 'crops')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        pl = patient.lower().split('-')\n",
    "        for i, s in enumerate(pl):\n",
    "            if i!=0 and s.startswith('im'):\n",
    "                pl[i] = s.replace('im', '')\n",
    "        crop_flag = True\n",
    "        for i, s in enumerate(pl):\n",
    "            if s != 's' and s != 'j':\n",
    "                continue\n",
    "            start, end = int(pl[i+1])-1, int(pl[i+2])\n",
    "            for j in range(start, end):\n",
    "                img = Image.open(os.path.join(image_path, f'{patient}_{j:04d}.png'))\n",
    "                img = np.array(img)\n",
    "                try:\n",
    "                    x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{j:04d}.txt'), s)\n",
    "                except:\n",
    "                    traceback.print_exc()\n",
    "                    crop_flag = False\n",
    "                else:#if crop_flag:\n",
    "                    crop = img[y1:y2, x1:x2]\n",
    "                    crop = Image.fromarray(crop)\n",
    "                    crop.save(os.path.join(crop_path, f'{patient}_{s}_{j:04d}.png'))\n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "\n",
    "epl1 = []\n",
    "epl2 = []\n",
    "epl3 = []\n",
    "\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2', epl1)\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2', epl2)\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "# print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2', epl3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86184053",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)\n",
    "print(len(epl2))\n",
    "print(epl2)\n",
    "print(len(epl3))\n",
    "print(epl3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c28d9f3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将切出的主动脉移动到一起\n",
    "def move_to_classify(input_root, output_path):\n",
    "    if not os.path.exists(output_path):\n",
    "        os.makedirs(output_path)\n",
    "    \n",
    "    for patient in os.listdir(input_root):\n",
    "        if os.path.isfile(os.path.join(input_root, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        for file in os.listdir(os.path.join(input_root, patient, 'crops')):\n",
    "            shutil.copy(os.path.join(input_root, patient, 'crops', file), output_path)\n",
    "\n",
    "move_to_classify('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2/', f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/1')\n",
    "move_to_classify('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2/', f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/2')\n",
    "move_to_classify('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2/', f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c386fdb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 切出范围外冗余为3的主动脉\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.2 < y < 0.8, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.2 < corr1[2] < 0.8, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.2 < corr2[2] < 0.8, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, 'images')\n",
    "        label_path = os.path.join(input_path, patient, 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, 'crops')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        pl = patient.lower().split('-')\n",
    "        for i, s in enumerate(pl):\n",
    "            if i!=0 and s.startswith('im'):\n",
    "                pl[i] = s.replace('im', '')\n",
    "        crop_flag = True\n",
    "        for i, s in enumerate(pl):\n",
    "            if s != 's' and s != 'j':\n",
    "                continue\n",
    "            start, end = int(pl[i+1])-1, int(pl[i+2])\n",
    "            for j in range(start-3, end+3):\n",
    "                try:\n",
    "                    img = Image.open(os.path.join(image_path, f'{patient}_{j:04d}.png'))\n",
    "                    img = np.array(img)\n",
    "                    x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{j:04d}.txt'), s)\n",
    "                except:\n",
    "                    traceback.print_exc()\n",
    "                    crop_flag = False\n",
    "                else:#if crop_flag:\n",
    "                    crop = img[y1:y2, x1:x2]\n",
    "                    crop = Image.fromarray(crop)\n",
    "                    if start <= j < end:\n",
    "                        crop.save(os.path.join(crop_path, f'{patient}_{s}_{j:04d}.png'))\n",
    "                    else:\n",
    "                        crop.save(os.path.join(crop_path, f'{patient}_{s}_{j:04d}_n.png'))\n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "\n",
    "epl1 = []\n",
    "epl2 = []\n",
    "epl3 = []\n",
    "\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/positive2', epl1)\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/imh2', epl2)\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau2', epl3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffdc2753",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)\n",
    "print(len(epl2))\n",
    "print(epl2)\n",
    "print(len(epl3))\n",
    "print(epl3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f12039a2",
   "metadata": {},
   "source": [
    "## 2.阴性数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d8a5f901",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing baixiaoxu-22-31-52-147****\n",
      "****Processing baojinjia-17-21-35-83****\n",
      "****Processing baoyanzu-21-25-42-93****\n",
      "****Processing bianyinqiao-31-38-59-137****\n",
      "****Processing bianzhaorong-17-22-38-92****\n",
      "****Processing caijingnan1-41-50-73-184****\n",
      "****Processing caiqishu-34-43-70-176****\n",
      "****Processing caiyouzhe-14-18-32-83****\n",
      "****Processing caoyuxia-16-21-35-87****\n",
      "****Processing chenfugui-36-46-76-149****\n",
      "****Processing chengzhiqiang-33-41-67-159****\n",
      "****Processing chenjian-26-30-44-89****\n",
      "****Processing chenjianhua-14-17-32-80****\n",
      "****Processing chenjianjun-25-34-65-175****\n",
      "****Processing chenjianping-15-22-41-91****\n",
      "****Processing chenjingjing-38-47-67-184****\n",
      "****Processing chenjinmei-12-17-34-80****\n",
      "****Processing chenpanyang-21-31-71-161****\n",
      "****Processing chenqiuying-13-17-30-78****\n",
      "****Processing chensaimu-34-44-78-166****\n",
      "****Processing chenxiufen-17-21-34-82****\n",
      "****Processing chenyanli-20-23-34-80****\n",
      "****Processing chenyinfen-18-22-38-81****\n",
      "****Processing cheshengying-25-34-64-139****\n",
      "****Processing denghuibin-15-19-35-84****\n",
      "****Processing dingbinghai-20-24-45-95****\n",
      "****Processing dongyonghong-14-19-34-85****\n",
      "****Processing dujinlian-17-20-36-82****\n",
      "****Processing fangchengfu-26-39-76-182****\n",
      "****Processing fangrurong-16-25-55-132****\n",
      "****Processing fangyungen-14-18-34-75****\n",
      "****Processing fanxinghua-41-48-69-144****\n",
      "****Processing fengdayan-30-38-69-146****\n",
      "****Processing fudianqin-15-19-33-78****\n",
      "****Processing geqingqing-33-41-68-169****\n",
      "****Processing gezhenghua-15-19-34-88****\n",
      "****Processing hanjinxiao-31-43-80-183****\n",
      "****Processing heguansheng-15-20-43-92****\n",
      "****Processing hexiena-18-22-38-82****\n",
      "****Processing huangguixian-24-28-45-101****\n",
      "****Processing huangjuzhen-13-17-29-77****\n",
      "****Processing huanglongsheng-23-28-46-92****\n",
      "****Processing huangweigen-24-32-62-138****\n",
      "****Processing huangxiang-22-26-42-84****\n",
      "****Processing hukaipin-29-39-73-159****\n",
      "****Processing jiangshengfang-19-23-34-79****\n",
      "****Processing jiangyuxiang-15-19-37-83****\n",
      "****Processing jinaixiang-34-41-63-149****\n",
      "****Processing jinangui-17-21-37-81****\n",
      "****Processing leijiuxiang-31-38-68-153****\n",
      "****Processing liangzhenwei-16-20-39-92****\n",
      "****Processing lidanfa-11-16-34-72****\n",
      "****Processing lideming-16-23-43-90****\n",
      "****Processing lifengyun-8-16-35-81****\n",
      "****Processing liguoyong-26-35-57-150****\n",
      "****Processing lihonggang-13-18-33-88****\n",
      "****Processing linaizhen-23-31-58-144****\n",
      "****Processing linshangjin-15-19-36-81****\n",
      "****Processing lirongchang-13-17-27-86****\n",
      "****Processing lishankui-17-25-56-142****\n",
      "****Processing liuxiaolin-39-49-81-176****\n",
      "****Processing lixiebing-17-20-37-87****\n",
      "****Processing liyingsheng-13-17-34-87****\n",
      "****Processing liyufang-32-40-68-138****\n",
      "****Processing liyunkun-29-33-51-99****\n",
      "****Processing lizufu-21-26-42-93****\n",
      "****Processing longpiaojiang-29-40-65-184****\n",
      "****Processing longyingdong-21-25-40-99****\n",
      "****Processing loucaidi-26-35-63-154****\n",
      "****Processing loudinghong-18-22-39-93****\n",
      "****Processing lucaifa-25-34-62-147****\n",
      "****Processing luguiyun-10-16-27-82****\n",
      "****Processing lulimian-24-31-64-135****\n",
      "****Processing lumin-13-17-34-85****\n",
      "****Processing luojun-22-29-51-139****\n",
      "****Processing luoshuijin-15-19-35-74****\n",
      "****Processing luweiqing-16-20-37-93****\n",
      "****Processing malingfeng-12-17-32-75****\n",
      "****Processing panxiangyang-45-53-97-196****\n",
      "****Processing puchenhu-18-21-33-89****\n",
      "****Processing qianaqin-18-26-52-141****\n",
      "****Processing qihuazhong-16-20-29-82****\n",
      "****Processing quzhuming-31-41-69-192****\n",
      "****Processing shenfengmei-29-36-58-130****\n",
      "****Processing shengmingzhen-10-13-26-75****\n",
      "****Processing shenyan-16-23-57-125****\n",
      "****Processing shenyunzhou-24-33-70-141****\n",
      "****Processing shouyunfang-20-26-42-136****\n",
      "****Processing taoxiuwei-26-34-54-137****\n",
      "****Processing tuyurong-15-18-34-88****\n",
      "****Processing wangahai-16-22-43-91****\n",
      "****Processing wangbeibei-28-38-62-148****\n",
      "****Processing wangfancun-28-39-72-175****\n",
      "****Processing wanghaifang-40-47-83-160****\n",
      "****Processing wangjianlin-15-21-37-82****\n",
      "****Processing wangjie-15-19-31-49****\n",
      "****Processing wangwei-16-20-36-87****\n",
      "****Processing wangying-30-36-77-162****\n",
      "****Processing wangyouxiang-21-27-41-131****\n",
      "****Processing wangyouxin-34-42-64-160****\n",
      "****Processing wangyuncai-14-20-34-91****\n",
      "****Processing weifengli-26-34-62-147****\n",
      "****Processing weihuiqiong-27-33-58-137****\n",
      "****Processing wenshuijuan-30-36-66-145****\n",
      "****Processing wuhanjiang-18-22-39-85****\n",
      "****Processing wumingshi2021061701-25-28-45-98****\n",
      "****Processing wumingshi2021080102-38-47-85-156****\n",
      "****Processing wuqinmei-24-32-52-140****\n",
      "****Processing wuruqing-49-60-96-153****\n",
      "****Processing wutiandi-21-28-54-120****\n",
      "****Processing wuyanxian-17-21-36-83****\n",
      "****Processing wuzongtang-29-36-76-141****\n",
      "****Processing xiabao-33-42-78-196****\n",
      "****Processing xiaofuxin-25-33-64-153****\n",
      "****Processing xiasuifu-12-18-30-84****\n",
      "****Processing xiyongmin-25-35-62-162****\n",
      "****Processing xurihua-26-34-61-145****\n",
      "****Processing xuyuetian-22-30-53-132****\n",
      "****Processing yangjinnan-31-38-67-146****\n",
      "****Processing yangshujuan-30-39-62-160****\n",
      "****Processing yangtangyong-15-19-35-85****\n",
      "****Processing yangzengrong-37-48-78-188****\n",
      "****Processing yesenfei-22-32-53-172****\n",
      "****Processing yinggenhua-36-45-72-174****\n",
      "****Processing yuanxiazhen-14-18-36-76****\n",
      "****Processing yuanxicai-35-43-85-169****\n",
      "****Processing yubaisong-25-33-65-152****\n",
      "****Processing yuyinglong-24-30-51-98****\n",
      "****Processing yuyunfu-26-33-66-148****\n",
      "****Processing yuzhixue-16-21-38-91****\n",
      "****Processing zengshengjiang-19-23-36-89****\n",
      "****Processing zhangguanghong-39-46-68-156****\n",
      "****Processing zhangguofeng-25-30-45-97****\n",
      "****Processing zhanghonghu-37-45-69-149****\n",
      "****Processing zhangjingjing-16-19-30-78****\n",
      "****Processing zhangjingzhong-53-59-85-174****\n",
      "****Processing zhangjunfei-25-33-52-125****\n",
      "****Processing zhangkesheng-27-34-63-144****\n",
      "****Processing zhangmeiqin-19-22-39-86****\n",
      "****Processing zhangsuyun-18-21-37-81****\n",
      "****Processing zhangyitian-26-32-55-148****\n",
      "****Processing zhanmin-18-23-37-88****\n",
      "****Processing zhaomin-11-15-32-86****\n",
      "****Processing zhaorong-33-42-71-163****\n",
      "****Processing zhaosuifang-23-34-61-154****\n",
      "****Processing zhaoyijun-17-20-33-82****\n",
      "****Processing zhengfahong-22-26-44-99****\n",
      "****Processing zhengjuying-16-19-33-86****\n",
      "****Processing zhengxuefei-21-24-43-86****\n",
      "****Processing zhengyouduo-23-27-41-83****\n",
      "****Processing zhoubo-19-26-41-137****\n",
      "****Processing zhouhaibin-30-39-74-185****\n",
      "****Processing zhoushan-17-20-39-78****\n",
      "****Processing zhoushijun-30-37-64-145****\n",
      "****Processing zhouweiqiang-19-24-42-90****\n",
      "****Processing zhouxinya-24-30-40-120****\n",
      "****Processing zhouxiushan-14-18-34-84****\n",
      "****Processing zhouyongjin-17-21-35-87****\n",
      "****Processing zhouzhangyun-26-35-61-136****\n",
      "****Processing zhudamin-33-41-70-156****\n",
      "****Processing zhumeizheng-34-42-69-167****\n",
      "****Processing zhuxiaoqin-22-29-50-130****\n",
      "****Processing zhuxinnan-18-26-49-129****\n",
      "****Processing zoudeling-11-15-30-84****\n",
      "****Processing baimeizhu-27-37-54-136****\n",
      "****Processing baoxialin-23-34-57-158****\n",
      "****Processing baoyanzu-15-20-31-87****\n",
      "****Processing caijinfei-34-42-51-155****\n",
      "****Processing caishiping-18-24-34-85****\n",
      "****Processing caoyuying-18-22-33-79****\n",
      "****Processing chenbin-20-26-35-85****\n",
      "****Processing chenchong-22-26-36-86****\n",
      "****Processing chendonghong-12-17-27-75****\n",
      "****Processing chengenye-13-18-28-79****\n",
      "****Processing chengshaoping-19-28-45-132****\n",
      "****Processing chengyahui-17-22-29-82****\n",
      "****Processing chenjunbing-22-29-34-89****\n",
      "****Processing chenlidong-18-23-32-88****\n",
      "****Processing chenlindi-16-22-34-78****\n",
      "****Processing chenliuer-23-32-43-140****\n",
      "****Processing chenmaojin-14-20-31-90****\n",
      "****Processing chenruohui-22-30-41-133****\n",
      "****Processing chenshigen-19-25-34-83****\n",
      "****Processing chenweiqian-22-31-45-131****\n",
      "****Processing chenyilin-21-26-36-86****\n",
      "****Processing chenyonghu-24-32-48-141****\n",
      "****Processing daicaihu-22-27-34-85****\n",
      "****Processing daijiandong-28-39-50-160****\n",
      "****Processing daixiaofeng-27-38-56-141****\n",
      "****Processing dengdeyu-18-27-43-152****\n",
      "****Processing dingshuigen-28-38-53-136****\n",
      "****Processing dongxingzhen-9-13-23-77****\n",
      "****Processing duyangchun-16-22-31-90****\n",
      "****Processing fangjinchai-19-23-31-81****\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing fangzhengyi-26-37-53-153****\n",
      "****Processing fangzijian-19-30-50-153****\n",
      "****Processing fanshiming-15-20-30-83****\n",
      "****Processing fengaiyuan-26-32-46-121****\n",
      "****Processing fengqiufen-14-19-27-75****\n",
      "****Processing fubaohua-30-41-62-149****\n",
      "****Processing fudiyang-22-30-46-128****\n",
      "****Processing fuhongliang-14-22-33-154****\n",
      "****Processing fujingjing-24-32-39-127****\n",
      "****Processing gaoanhui-17-22-31-85****\n",
      "****Processing gaoqigen-36-45-63-148****\n",
      "****Processing gaowenfu-16-26-50-141****\n",
      "****Processing gaozhaocheng-19-26-42-90****\n",
      "****Processing gegaocai-20-24-32-88****\n",
      "****Processing gejinhua-21-26-35-82****\n",
      "****Processing gezhidong-21-27-38-93****\n",
      "****Processing guohao-25-30-38-94****\n",
      "****Processing guoyafang-24-29-52-136****\n",
      "****Processing hanshufang-28-35-53-137****\n",
      "****Processing hanweijie-19-25-37-90****\n",
      "****Processing hebaomin-28-37-54-145****\n",
      "****Processing henglangye-15-20-25-83****\n",
      "****Processing hongxia-17-34-43-122****\n",
      "****Processing huanghuaide-23-34-57-153****\n",
      "****Processing huangjiabao-22-27-33-53****\n",
      "****Processing huangxuena-17-22-27-75****\n",
      "****Processing huangzhanzhang-25-33-46-134****\n",
      "****Processing huangzhaoxun-29-41-62-152****\n",
      "****Processing huaweiqin-15-25-33-77****\n",
      "****Processing humingdong-18-22-29-85****\n",
      "****Processing huxiangxin-19-24-34-86****\n",
      "****Processing huxianhua-32-41-55-143****\n",
      "****Processing huyongxian-18-25-38-88****\n",
      "****Processing huyunhua-15-20-28-78****\n",
      "****Processing huzaoheng-16-23-34-67****\n",
      "****Processing huzhengjin-19-27-48-150****\n",
      "****Processing jiangchengsan-25-34-50-133****\n",
      "****Processing jiangguanshui-16-21-31-85****\n",
      "****Processing jiangjianrong-16-21-31-82****\n",
      "****Processing jiangjiaqi-23-30-39-140****\n",
      "****Processing jiangjinfeng-26-39-58-152****\n",
      "****Processing jiangxiangzhong-15-20-29-83****\n",
      "****Processing jiangzhongshui-24-32-48-134****\n",
      "****Processing jiangzhuxian-10-16-24-75****\n",
      "****Processing jingchunya-12-17-25-82****\n",
      "****Processing jinjianrong-19-24-32-86****\n",
      "****Processing jinlanhua-24-32-43-131****\n",
      "****Processing jinruiyue-13-18-28-80****\n",
      "****Processing jinshuigen-18-23-36-79****\n",
      "****Processing jinxianqing-15-23-37-81****\n",
      "****Processing jinyongfa-25-29-37-92****\n",
      "****Processing jinyu-19-29-44-146****\n",
      "****Processing jinzhaoxiang-28-37-55-126****\n",
      "****Processing kerengang-25-30-41-91****\n",
      "****Processing kongfansheng-20-26-38-87****\n",
      "****Processing kongxiangang-24-28-38-97****\n",
      "****Processing kongxiangwen-12-18-32-86****\n",
      "****Processing laijinhu-14-21-34-83****\n",
      "****Processing liangmingfen-19-24-34-84****\n",
      "****Processing liaoxiuhua-36-44-55-144****\n",
      "****Processing lidong-16-21-25-83****\n",
      "****Processing lihongmei-11-16-33-83****\n",
      "****Processing lijunhua-15-21-27-82****\n",
      "****Processing lining-17-21-28-81****\n",
      "****Processing lintianye-18-28-46-136****\n",
      "****Processing linyantian-22-32-54-146****\n",
      "****Processing linyaoqin-15-20-30-89****\n",
      "****Processing liqiang-17-22-27-83****\n",
      "****Processing lishengfu-19-25-37-95****\n",
      "****Processing lisucheng-22-26-36-89****\n",
      "****Processing liubaojian-23-31-49-143****\n",
      "****Processing liulixuan-16-22-27-87****\n",
      "****Processing liulunyong-26-35-49-155****\n",
      "****Processing liutongkang-12-18-25-84****\n",
      "****Processing liuwangyang-19-24-32-87****\n",
      "****Processing liuweizhen-15-20-31-80****\n",
      "****Processing lixian-19-24-32-83****\n",
      "****Processing lixiaolang-17-21-30-83****\n",
      "****Processing lixinxi-16-23-33-86****\n",
      "****Processing lixiuhua-28-37-51-135****\n",
      "****Processing liyongjian-16-22-31-86****\n",
      "****Processing liyunbo-21-28-45-150****\n",
      "****Processing louguiqin-19-28-50-150****\n",
      "****Processing luguojing-27-36-47-141****\n",
      "****Processing lujuquan-25-35-50-126****\n",
      "****Processing luoguangrong-23-32-51-129****\n",
      "****Processing luoguidi-21-32-51-140****\n",
      "****Processing luojinfeng-30-38-53-149****\n",
      "****Processing luqiaoling-12-17-26-73****\n",
      "****Processing lushuyun-19-30-47-149****\n",
      "****Processing luwenjun-20-30-44-141****\n",
      "****Processing luyuefeng-24-28-40-94****\n",
      "****Processing luzhiwei-19-25-36-88****\n",
      "****Processing lvbiqiang-34-42-63-143****\n",
      "****Processing lvnanxian-17-23-32-84****\n",
      "****Processing lvyinhuan-34-43-58-151****\n",
      "****Processing lvyundi-29-36-45-131****\n",
      "****Processing maguojin-27-37-56-141****\n",
      "****Processing mahuiwu-10-14-23-84****\n",
      "****Processing majiangen-17-22-34-87****\n",
      "****Processing majinmu-30-41-62-158****\n",
      "****Processing maoshuifa-30-40-60-144****\n",
      "****Processing maoshuijuan-12-17-23-80****\n",
      "****Processing maowenli-17-22-32-76****\n",
      "****Processing mawei-15-20-28-85****\n",
      "****Processing meipingan-36-45-60-137****\n",
      "****Processing menghaixing-17-28-41-136****\n",
      "****Processing miaohefen-19-27-40-134****\n",
      "****Processing miaoxiaojun-35-46-65-146****\n",
      "****Processing muchunxian-15-21-31-81****\n",
      "****Processing nabuqi-13-18-26-79****\n",
      "****Processing ningdegen-28-38-61-140****\n",
      "****Processing ningjiangui-18-23-30-83****\n",
      "****Processing nisixiang-11-17-25-81****\n",
      "****Processing panandan-16-21-31-81****\n",
      "****Processing panhongfu-37-48-75-151****\n",
      "****Processing panming-17-22-30-90****\n",
      "****Processing panxiuping-29-38-55-142****\n",
      "****Processing panyuda-29-36-52-102****\n",
      "****Processing pengguizhen-30-41-59-160****\n",
      "****Processing pengyufei-31-39-51-154****\n",
      "****Processing puguanghui-12-18-25-74****\n",
      "****Processing qianliujiu-9-15-24-75****\n",
      "****Processing qiuwei-31-39-53-138****\n",
      "****Processing qizhikun-29-37-57-130****\n",
      "****Processing qubo-14-19-29-82****\n",
      "****Processing renjiali-34-42-52-138****\n",
      "****Processing renlaifa-19-30-53-153****\n",
      "****Processing renxiuyuan-18-23-31-79****\n",
      "****Processing shanziliang-13-19-30-90****\n",
      "****Processing shanzixian-30-39-59-153****\n",
      "****Processing shaobingsheng-13-19-27-80****\n",
      "****Processing shaochuanlai-16-22-36-83****\n",
      "****Processing shenguilin-18-25-35-53****\n",
      "****Processing shenhaiqin-20-28-38-129****\n",
      "****Processing shenxingen-29-40-60-150****\n",
      "****Processing shenxuemei-15-21-28-79****\n",
      "****Processing shenxuhong-20-24-34-82****\n",
      "****Processing shenyaping-30-40-57-139****\n",
      "****Processing shenyuzhen-11-16-25-77****\n",
      "****Processing shenziming-17-24-34-84****\n",
      "****Processing shiburen-15-20-29-91****\n",
      "****Processing shizhifu-37-47-58-147****\n",
      "****Processing songzongwei-20-26-39-94****\n",
      "****Processing sundelin-15-21-31-90****\n",
      "****Processing sunlutao-14-23-70-127****\n",
      "****Processing sunyunxian-11-17-23-75****\n",
      "****Processing tangchunhua-10-16-20-76****\n",
      "****Processing tangshuiming-33-44-68-153****\n",
      "****Processing taofeng-18-22-28-81****\n",
      "****Processing taonan-19-24-37-87****\n",
      "****Processing taoquanying-13-18-30-83****\n",
      "****Processing taowenming-23-32-59-138****\n",
      "****Processing tongbo-25-35-51-153****\n",
      "****Processing tongchichang-16-21-35-88****\n",
      "****Processing tongxijie-33-42-61-178****\n",
      "****Processing wangaifang-13-19-28-74****\n",
      "****Processing wangbaoyu-7-12-21-75****\n",
      "****Processing wangbingdao-26-36-55-132****\n",
      "****Processing wangdahua-19-24-33-89****\n",
      "****Processing wangdandan-32-39-50-142****\n",
      "****Processing wangdayang-3-14-41-137****\n",
      "****Processing wangdehao-40-51-65-159****\n",
      "****Processing wangfeng-14-19-31-85****\n",
      "****Processing wangguojian-20-25-39-86****\n",
      "****Processing wangguolong-16-21-33-84****\n",
      "****Processing wanghongchao-33-42-69-153****\n",
      "****Processing wangjiajin-35-46-69-148****\n",
      "****Processing wangjian-17-24-32-88****\n",
      "****Processing wangjinlie-26-33-45-87****\n",
      "****Processing wangkehuai-47-57-80-157****\n",
      "****Processing wangligang-25-36-55-154****\n",
      "****Processing wangluhua-18-23-32-87****\n",
      "****Processing wangmingxuan-35-45-67-157****\n",
      "****Processing wangronglin-16-21-28-81****\n",
      "****Processing wangsonglin-19-24-27-86****\n",
      "****Processing wangwei-32-39-50-139****\n",
      "****Processing wangwuzhuang-28-33-41-98****\n",
      "****Processing wangxinhui-18-23-31-85****\n",
      "****Processing wangyunxian-18-27-39-134****\n",
      "****Processing wangyuzhi-29-38-55-156****\n",
      "****Processing weiliping-14-19-29-82****\n",
      "****Processing wenshuinong-24-33-55-141****\n",
      "****Processing wufenai-9-15-28-81****\n",
      "****Processing wugaoyou-12-17-24-81****\n",
      "****Processing wuguorong-30-39-51-140****\n",
      "****Processing wujianguo-14-20-29-89****\n",
      "****Processing wumingshi2021040501-17-22-30-81****\n",
      "****Processing wutianshun-12-17-26-80****\n",
      "****Processing wuyunfang-20-25-34-85****\n",
      "****Processing wuzili-13-19-28-72****\n",
      "****Processing xiajinying-22-32-46-134****\n",
      "****Processing xiangzhaochong-15-20-31-78****\n",
      "****Processing xiaxianlin-5-9-15-71****\n",
      "****Processing xiaxinghua-17-22-33-89****\n",
      "****Processing xiegenjun-21-26-34-93****\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****Processing xiemeiying-21-29-41-127****\n",
      "****Processing xieshangyong-14-20-27-87****\n",
      "****Processing xiezhaoqin-17-21-29-81****\n",
      "****Processing xinyongsheng-13-18-26-86****\n",
      "****Processing xucaizhen-27-37-51-131****\n",
      "****Processing xudingguo-21-25-38-89****\n",
      "****Processing xuguangtai-16-21-36-91****\n",
      "****Processing xushengwu-31-41-60-150****\n",
      "****Processing xuxuee-22-30-45-123****\n",
      "****Processing xuyizhen-22-32-47-149****\n",
      "****Processing yananfen-29-39-56-146****\n",
      "****Processing yanchunhong-36-45-64-155****\n",
      "****Processing yangqingyue-31-39-50-128****\n",
      "****Processing yangxiaohui-20-28-39-125****\n",
      "****Processing yangyuqi-34-44-60-157****\n",
      "****Processing yangzengrong-28-40-58-161****\n",
      "****Processing yangzhengfu-18-22-32-75****\n",
      "****Processing yangzhengqian-16-21-26-78****\n",
      "****Processing yaohuiping-16-21-29-79****\n",
      "****Processing yaojingen-20-25-31-81****\n",
      "****Processing yegenxiang-11-17-30-80****\n",
      "****Processing yepusheng-13-18-27-79****\n",
      "****Processing yeronghua-21-32-49-149****\n",
      "****Processing yeshanhui-33-43-55-143****\n",
      "****Processing yeweigen-17-21-29-84****\n",
      "****Processing yimuyun-15-19-28-75****\n",
      "****Processing yiqiang-16-21-30-78****\n",
      "****Processing yuaiyuan-13-18-29-84****\n",
      "****Processing yuanashui-27-38-57-141****\n",
      "****Processing yuanjuhe-11-16-27-76****\n",
      "****Processing yuankunfa-27-33-44-89****\n",
      "****Processing yuanyongjin-12-18-27-81****\n",
      "****Processing yuguangyang-18-23-33-90****\n",
      "****Processing yujixin-32-41-62-143****\n",
      "****Processing yukang-17-22-27-82****\n",
      "****Processing yukang-27-36-46-135****\n",
      "****Processing yumengting-19-26-36-123****\n",
      "****Processing yupengjie-26-34-46-134****\n",
      "****Processing yushengfu-31-40-56-154****\n",
      "****Processing yuweiliang-29-35-47-104****\n",
      "****Processing yuxiaotu-20-25-36-90****\n",
      "****Processing zengchunsheng-23-30-41-128****\n",
      "****Processing zhangchao-13-18-29-81****\n",
      "****Processing zhangdanye-31-38-50-143****\n",
      "****Processing zhangduofeng-12-17-28-55****\n",
      "****Processing zhangfagen-22-27-37-92****\n",
      "****Processing zhangguofeng-21-27-35-90****\n",
      "****Processing zhanghanmin-13-19-27-76****\n",
      "****Processing zhanghuanquan-17-22-29-75****\n",
      "****Processing zhanglanxian-19-28-48-129****\n",
      "****Processing zhangmingfa-22-27-37-93****\n",
      "****Processing zhangqingrong-32-43-73-161****\n",
      "****Processing zhangxianjun-16-23-31-82****\n",
      "****Processing zhangyao-17-22-29-83****\n",
      "****Processing zhangyulin-20-25-40-93****\n",
      "****Processing zhaofuchang-16-21-30-80****\n",
      "****Processing zhaoshunshou-30-38-53-139****\n",
      "****Processing zhaoyouyang-24-34-41-125****\n",
      "****Processing zhengguanhan-17-23-34-79****\n",
      "****Processing zhengjilang-23-33-49-135****\n",
      "****Processing zhengronghua-21-30-44-127****\n",
      "****Processing zhengwanhua-29-40-59-156****\n",
      "****Processing zhengwenyan-15-20-29-82****\n",
      "****Processing zhengyingu-14-18-26-76****\n",
      "****Processing zhengzaidi-22-34-51-153****\n",
      "****Processing zhensuzhen-20-25-34-90****\n",
      "****Processing zhongxiaoxiang-30-40-53-139****\n",
      "****Processing zhouguoming-20-25-30-82****\n",
      "****Processing zhouhongqiong-31-42-54-147****\n",
      "****Processing zhoujun-12-16-24-77****\n",
      "****Processing zhoulijun-18-23-35-93****\n",
      "****Processing zhouwenming-40-48-67-147****\n",
      "****Processing zhouxianhang-13-17-26-78****\n",
      "****Processing zhouyixiao-22-28-40-83****\n",
      "****Processing zhouyongyan-21-33-51-136****\n",
      "****Processing zhouzhangnv-12-18-31-80****\n",
      "****Processing zhuguizhou-18-23-34-85****\n",
      "****Processing zhuguofang-15-20-32-85****\n",
      "****Processing zhujianfu-16-22-28-87****\n",
      "****Processing zhujianfu-30-42-62-154****\n",
      "****Processing zhukuanwen-15-21-29-83****\n",
      "****Processing zhuodepeng-9-14-23-78****\n",
      "****Processing zhuyanjie-20-25-35-89****\n"
     ]
    }
   ],
   "source": [
    "# 将dcm文件根据窗宽窗位转化为png图片\n",
    "def generate_image(input_folder):\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        for scan in os.listdir(os.path.join(input_folder, patient)):\n",
    "            if scan != '1':\n",
    "                continue\n",
    "            name = patient #name = patient.split('-')[0]\n",
    "            image_path = os.path.join(input_folder, patient, scan, f'images_{lower_b}_{upper_b}')\n",
    "            if os.path.exists(image_path):\n",
    "                shutil.rmtree(image_path)\n",
    "            os.mkdir(image_path)\n",
    "\n",
    "            ct = load_scan(os.path.join(input_folder, patient, scan))\n",
    "            print_flag = False\n",
    "            for i in range(len(ct)):\n",
    "                img = ct[i].pixel_array.astype(np.int16)\n",
    "                intercept = ct[i].RescaleIntercept\n",
    "                slope = ct[i].RescaleSlope\n",
    "                if slope != 1:\n",
    "                    img = (slope * img.astype(np.float64)).astype(np.int16)\n",
    "                img += np.int16(intercept)\n",
    "                img = np.clip(img, lower_b, upper_b)\n",
    "                img = ((img-lower_b)/(upper_b-lower_b)*255).astype(np.uint8)\n",
    "                img = Image.fromarray(img)\n",
    "                if img.height != img.width and not print_flag:\n",
    "                    print(patient, 'height not equal to width\\n')\n",
    "                    print_flag = True\n",
    "                img.save(os.path.join(image_path, f'{name}_{i:04d}.png'))\n",
    "\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-9-17-negative/')\n",
    "generate_image('/nfs3-p2/zsxm/dataset/2021-9-29-negative/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4709fcfb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将各个病例中的png图片文件夹统一移动到一起供yolov5检测\n",
    "def move_together_for_detect(input_folder, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    root_name = input_folder.split('/')[-1] if input_folder.split('/')[-1] != '' else input_folder.split('/')[-2]\n",
    "    dst_path = os.path.join(dst_path, root_name)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        if os.path.exists(os.path.join(dst_path, name)):\n",
    "            print(f\"\\tremove {os.path.join(dst_path, name)}\")\n",
    "            shutil.rmtree(os.path.join(dst_path, name))\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '1', f'images_{lower_b}_{upper_b}'), os.path.join(dst_path, name))\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "\n",
    "move_together_for_detect('/nfs3-p2/zsxm/dataset/2021-9-17-negative', '/nfs3-p1/zsxm/dataset/9_detect/')\n",
    "move_together_for_detect('/nfs3-p2/zsxm/dataset/2021-9-29-negative', '/nfs3-p1/zsxm/dataset/9_detect/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1da4cac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-17-negative', '/nfs3-p1/zsxm/dataset/9_detect/pred')\n",
    "shutil.move('/disk1/zsxm/pythonWorkspace/yolov5/runs/detect/2021-9-29-negative', '/nfs3-p1/zsxm/dataset/9_detect/pred')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6652d16c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用在更换窗宽窗位的时候，因为已经经过yolov5的检测生成了标签，故直接移动图片到images文件夹下即可\n",
    "def directly_move_to_image_label(input_folder, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "\n",
    "    for patient in sorted(os.listdir(input_folder)):\n",
    "        if os.path.isfile(os.path.join(input_folder, patient)):\n",
    "            continue\n",
    "        print(f'****Processing {patient}****')\n",
    "        name = patient #name = patient.split('-')[0]\n",
    "        image_path = os.path.join(dst_path, name, 'images')\n",
    "        if os.path.exists(image_path):\n",
    "            print(f\"\\tremove {image_path}\")\n",
    "            shutil.rmtree(image_path)\n",
    "\n",
    "        try:\n",
    "            shutil.copytree(os.path.join(input_folder, patient, '1', f'images_{lower_b}_{upper_b}'), image_path)\n",
    "        except:\n",
    "            traceback.print_exc()\n",
    "            \n",
    "directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-17-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')\n",
    "directly_move_to_image_label('/nfs3-p1/zsxm/dataset/2021-9-29-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0d57e3a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将移动到待测文件夹中的原图移动到images文件夹\n",
    "def move_ori_image_to_sigle_folder(input_path, dst_path):\n",
    "    if not os.path.exists(dst_path):\n",
    "        os.mkdir(dst_path)\n",
    "    for patient in os.listdir(input_path):\n",
    "        print(f'****Processing {patient}****')\n",
    "        if not os.path.exists(os.path.join(dst_path, patient)):\n",
    "            os.mkdir(os.path.join(dst_path, patient))\n",
    "        image_path = os.path.join(dst_path, patient, 'images')\n",
    "        if os.path.exists(image_path):\n",
    "            shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for img in os.listdir(os.path.join(input_path, patient)):\n",
    "            if os.path.isfile(os.path.join(input_path, patient, img)):\n",
    "                shutil.move(os.path.join(input_path, patient, img), os.path.join(image_path, img))\n",
    "        os.rmdir(os.path.join(input_path, patient))\n",
    "    os.rmdir(input_path)\n",
    "    \n",
    "move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-17-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')\n",
    "move_ori_image_to_sigle_folder('/nfs3-p1/zsxm/dataset/9_detect/2021-9-29-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e44da286",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 将yolov5的检测结果（标签和带bounding box的图像）移动到相应文件夹\n",
    "def move_detect_result_to_folder(input_path, output_path):\n",
    "    for patient in os.listdir(input_path):\n",
    "        print(f'Processing {patient}')\n",
    "        image_path = os.path.join(output_path, patient, 'pred_images')\n",
    "        if os.path.exists(image_path):\n",
    "            shutil.rmtree(image_path)\n",
    "        os.mkdir(image_path)\n",
    "        for file in os.listdir(os.path.join(input_path, patient)):\n",
    "            if os.path.isfile(os.path.join(input_path, patient, file)):\n",
    "                shutil.move(os.path.join(input_path, patient, file), os.path.join(image_path, file))\n",
    "            elif os.path.isdir(os.path.join(input_path, patient, file)):\n",
    "                if os.path.exists(os.path.join(output_path, patient, file)):\n",
    "                    shutil.rmtree(os.path.join(output_path, patient, file))\n",
    "                shutil.move(os.path.join(input_path, patient, file), os.path.join(output_path, patient, file))\n",
    "        os.rmdir(os.path.join(input_path, patient))\n",
    "    os.rmdir(input_path)\n",
    "                \n",
    "move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-17-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')\n",
    "move_detect_result_to_folder('/nfs3-p1/zsxm/dataset/9_detect/pred/2021-9-29-negative/', '/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21f52558",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 切出主动脉,这里有问题啊，branch_end前后0.3的切片切不出来，建议以后更改为和下面一样的方案\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.2 < y < 0.8, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.2 < corr1[2] < 0.8, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.2 < corr2[2] < 0.8, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, 'images')\n",
    "        label_path = os.path.join(input_path, patient, 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, 'crops')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        pl = patient.split('-')\n",
    "        assert len(pl) == 5\n",
    "        aorta_start, branch_start, branch_end, aorta_end = int(pl[1])-1, int(pl[2])-1, int(pl[3])-1, int(pl[4])-1\n",
    "        crop_flag = True\n",
    "        offset = branch_end - branch_start\n",
    "        start, end = branch_start + int(0.1*offset), branch_end - int(0.2*offset)\n",
    "        for i in range(start, end):\n",
    "            img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "            img = np.array(img)\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 's')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop.save(os.path.join(crop_path, f'{patient}_s_{i:04d}.png'))\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 'j')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop.save(os.path.join(crop_path, f'{patient}_j_{i:04d}.png'))\n",
    "        offset = aorta_end - branch_end\n",
    "        start, end = branch_end + int(0.1*offset), aorta_end - int(0.2*offset)\n",
    "        for i in range(start, end):\n",
    "            img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "            img = np.array(img)\n",
    "            try:\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 'j')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                crop.save(os.path.join(crop_path, f'{patient}_j_{i:04d}.png')) \n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "            \n",
    "epl1 = []\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative', epl1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "513a9ba1",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d05ae35",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# # 将切出的主动脉移动到一起\n",
    "# def move_to_classify(input_root, output_path):\n",
    "#     if not os.path.exists(output_path):\n",
    "#         os.makedirs(output_path)\n",
    "    \n",
    "#     for patient in os.listdir(input_root):\n",
    "#         if os.path.isfile(os.path.join(input_root, patient)):\n",
    "#             continue\n",
    "#         print(f'****Processing {patient}****')\n",
    "#         for file in os.listdir(os.path.join(input_root, patient, 'crops')):\n",
    "#             shutil.copy(os.path.join(input_root, patient, 'crops', file), output_path)\n",
    "            \n",
    "# move_to_classify('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative/', f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/0/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03aad1f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切出范围外冗余为3的主动脉\n",
    "def find_coordinate(height, width, label_file, aorta):\n",
    "    with open(label_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    assert len(lines) <= 2, f'label.txt应该存储不多于2个label：{label_file.split(\"/\")[-1]}'\n",
    "    if len(lines) == 1:\n",
    "        assert aorta == 'j', f'如果只有一个label那么此时应为降主动脉, 但实际为{aorta}：{label_file.split(\"/\")[-1]}'\n",
    "        corr = list(map(lambda x: float(x), lines[0].split()))\n",
    "        x, y, w, h = corr[1], corr[2], corr[3], corr[4]\n",
    "        assert 0.25 < x < 0.75 and 0.2 < y < 0.8, f'边界框中心({x}, {y})出界：{label_file.split(\"/\")[-1]}'\n",
    "    else:\n",
    "        corr1, corr2 = list(map(lambda x: float(x), lines[0].split())), list(map(lambda x: float(x), lines[1].split()))\n",
    "        assert 0.25 < corr1[1] < 0.75 and 0.2 < corr1[2] < 0.8, f'边界框1中心({corr1[1]}, {corr1[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        assert 0.25 < corr2[1] < 0.75 and 0.2 < corr2[2] < 0.8, f'边界框2中心({corr2[1]}, {corr2[2]})出界：{label_file.split(\"/\")[-1]}'\n",
    "        if aorta == 's':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] < corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        elif aorta == 'j':\n",
    "            x, y, w, h = (corr1[1], corr1[2], corr1[3], corr1[4]) if corr1[2] > corr2[2] else (corr2[1], corr2[2], corr2[3], corr2[4])\n",
    "        else:\n",
    "            raise Exception(f'aorta 应该为\"s\"或\"j\"其中之一: {label_file.split(\"/\")[-1]}')\n",
    "    w, h = int(width*w), int(height*h)\n",
    "    w, h = max(w, h), max(w, h)\n",
    "    return int(width*x-w/2), int(height*y-h/2), int(width*x+w/2+1), int(height*y+h/2+1)\n",
    "\n",
    "def crop_images(input_path, error_patient_list):\n",
    "    for patient in sorted(os.listdir(input_path)):\n",
    "        print(f'******Processing {patient}******')\n",
    "        image_path = os.path.join(input_path, patient, 'images')\n",
    "        label_path = os.path.join(input_path, patient, 'labels')\n",
    "        crop_path = os.path.join(input_path, patient, 'crops')\n",
    "        if os.path.exists(crop_path):\n",
    "            shutil.rmtree(crop_path)\n",
    "        os.mkdir(crop_path)\n",
    "        pl = patient.split('-')\n",
    "        assert len(pl) == 5\n",
    "        aorta_start, branch_start, branch_end, aorta_end = int(pl[1])-1, int(pl[2])-1, int(pl[3])-1, int(pl[4])-1\n",
    "        crop_flag = True\n",
    "        offset = branch_end - branch_start\n",
    "        start, end = branch_start + int(0.1*offset), branch_end - int(0.2*offset)\n",
    "        for i in range(start-3, end+3):\n",
    "            try:\n",
    "                img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "                img = np.array(img)\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 's')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                if start <= i < end:\n",
    "                    crop.save(os.path.join(crop_path, f'{patient}_s_{i:04d}.png'))\n",
    "                else:\n",
    "                    crop.save(os.path.join(crop_path, f'{patient}_s_{i:04d}_n.png'))\n",
    "        offset = aorta_end - branch_start\n",
    "        start, end = branch_start + int(0.05*offset), aorta_end - int(0.1*offset)\n",
    "        for i in range(start-3, end+3):\n",
    "            try:\n",
    "                img = Image.open(os.path.join(image_path, f'{patient}_{i:04d}.png'))\n",
    "                img = np.array(img)\n",
    "                x1, y1, x2, y2 = find_coordinate(*img.shape[0:2], os.path.join(label_path, f'{patient}_{i:04d}.txt'), 'j')\n",
    "            except:\n",
    "                traceback.print_exc()\n",
    "                crop_flag = False\n",
    "            else:#if crop_flag:\n",
    "                crop = img[y1:y2, x1:x2]\n",
    "                crop = Image.fromarray(crop)\n",
    "                if start <= i < end:\n",
    "                    crop.save(os.path.join(crop_path, f'{patient}_j_{i:04d}.png'))\n",
    "                else:\n",
    "                    crop.save(os.path.join(crop_path, f'{patient}_j_{i:04d}_n.png'))\n",
    "        if not crop_flag:\n",
    "            #print('Delete crop_path')\n",
    "            #shutil.rmtree(crop_path)\n",
    "            error_patient_list.append(patient)\n",
    "            \n",
    "epl1 = []\n",
    "crop_images('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative', epl1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac63d3db",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(epl1))\n",
    "print(epl1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91313a4",
   "metadata": {},
   "source": [
    "## 3.固定训练集和测试集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8474cb60",
   "metadata": {},
   "source": [
    "### 3.1 阳性数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e00af454",
   "metadata": {},
   "outputs": [],
   "source": [
    "patient_roots = ['/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau']\n",
    "patient_list = []\n",
    "for patient_root in patient_roots:\n",
    "    for patient in sorted(os.listdir(patient_root)):\n",
    "        if os.path.isdir(os.path.join(patient_root, patient)):\n",
    "            #patient_list.append(os.path.join(patient_root, patient))\n",
    "            patient_list.append(patient)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e119e915",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(7987)\n",
    "val_patient_list = random.sample(patient_list, int(len(patient_list)*0.2))\n",
    "train_patient_list = [p for p in patient_list if p not in val_patient_list]#list(set(patient_list) - set(val_patient_list))\n",
    "train_patient_list.sort()\n",
    "val_patient_list.sort()\n",
    "print(len(train_patient_list), len(val_patient_list), len(train_patient_list)+len(val_patient_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea6c26d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "ori_train_list = set()\n",
    "ori_val_list = set()\n",
    "for img in os.listdir('/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/train/3'):\n",
    "    patient = img.split('_')[0]\n",
    "    ori_train_list.add(patient)\n",
    "for img in os.listdir('/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/val/3'):\n",
    "    patient = img.split('_')[0]\n",
    "    ori_val_list.add(patient)\n",
    "ori_train_list = sorted(list(ori_train_list))\n",
    "ori_val_list = sorted(list(ori_val_list))\n",
    "print(len(ori_train_list), len(ori_val_list), len(ori_train_list)+len(ori_val_list), len(patient_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45d6aad9",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_patient_list = [os.path.join('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau', p) for p in ori_train_list]\n",
    "val_patient_list = [os.path.join('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/pau', p) for p in ori_val_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0b2879e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def move_to_classify(patient_list, output_path):\n",
    "    if not os.path.exists(output_path):\n",
    "        os.makedirs(output_path)\n",
    "    \n",
    "    for patient in patient_list:\n",
    "        print(f'****Processing {patient}****')\n",
    "        for file in os.listdir(os.path.join(patient, 'crops')):\n",
    "            shutil.copy(os.path.join(patient, 'crops', file), output_path)\n",
    "            \n",
    "move_to_classify(train_patient_list, f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/wrong_basic_img/train/3/')\n",
    "move_to_classify(val_patient_list, f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/wrong_basic_img/val/3/')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ad2fc8d",
   "metadata": {},
   "source": [
    "### 3.2阴性数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13629389",
   "metadata": {},
   "outputs": [],
   "source": [
    "patient_roots = ['/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative/']\n",
    "patient_list = []\n",
    "for patient_root in patient_roots:\n",
    "    for patient in sorted(os.listdir(patient_root)):\n",
    "        if os.path.isdir(os.path.join(patient_root, patient)):\n",
    "            #patient_list.append(os.path.join(patient_root, patient))\n",
    "            patient_list.append(patient)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0de966b",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(7888)\n",
    "val_patient_list = random.sample(patient_list, int(len(patient_list)*0.2))\n",
    "train_patient_list = [p for p in patient_list if p not in val_patient_list]#list(set(patient_list) - set(val_patient_list))\n",
    "train_patient_list.sort()\n",
    "val_patient_list.sort()\n",
    "print(len(train_patient_list), len(val_patient_list), len(train_patient_list)+len(val_patient_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbe77534",
   "metadata": {},
   "outputs": [],
   "source": [
    "ori_train_list = set()\n",
    "ori_val_list = set()\n",
    "for img in os.listdir('/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/train/0'):\n",
    "    patient = img.split('_')[0]\n",
    "    ori_train_list.add(patient)\n",
    "for img in os.listdir('/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/val/0'):\n",
    "    patient = img.split('_')[0]\n",
    "    ori_val_list.add(patient)\n",
    "ori_train_list = sorted(list(ori_train_list))\n",
    "ori_val_list = sorted(list(ori_val_list))\n",
    "print(len(ori_train_list), len(ori_val_list), len(ori_train_list)+len(ori_val_list), len(patient_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f3379eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_patient_list = [os.path.join('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative', p) for p in ori_train_list]\n",
    "val_patient_list = [os.path.join('/nfs3-p1/zsxm/dataset/aorta_ct_img_label/negative', p) for p in ori_val_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5755f30",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def move_to_classify(patient_list, output_path):\n",
    "    if not os.path.exists(output_path):\n",
    "        os.makedirs(output_path)\n",
    "    \n",
    "    for patient in patient_list:\n",
    "        print(f'****Processing {patient}****')\n",
    "        for file in os.listdir(os.path.join(patient, 'crops')):\n",
    "            shutil.copy(os.path.join(patient, 'crops', file), output_path)\n",
    "            \n",
    "move_to_classify(train_patient_list, f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/wrong_basic_img/train/0/')\n",
    "move_to_classify(val_patient_list, f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/wrong_basic_img/val/0/')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91b8a5ea",
   "metadata": {},
   "source": [
    "## 4.检查并统计crop图像尺寸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0dec45e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "from numpy.lib.function_base import median\n",
    "\n",
    "paths = [f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/0/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/val/0/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/1/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/val/1/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/2/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/val/2/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/train/3/',\n",
    "         f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/center/val/3/',\n",
    "        ]\n",
    "\n",
    "size_list = []\n",
    "\n",
    "count = 0\n",
    "for path in paths:\n",
    "    path_count = 0\n",
    "    for f in os.listdir(path):\n",
    "        path_count += 1\n",
    "        img = Image.open(os.path.join(path, f))\n",
    "        size_list.append(img.height)\n",
    "        if img.height != img.width:\n",
    "            print(f'{os.path.join(path, f)}: height:{img.height}, width:{img.width}')\n",
    "            count += 1\n",
    "    print(f'{path}: {path_count}')\n",
    "\n",
    "size_list.sort()\n",
    "\n",
    "sum = 0\n",
    "hash_list = {}\n",
    "for size in size_list:\n",
    "    sum += size\n",
    "    hash_list[size] = hash_list.get(size, 0) + 1\n",
    "\n",
    "avg = sum / len(size_list)\n",
    "\n",
    "key = len(size_list) // 2\n",
    "med = (size_list[key] + size_list[-key-1]) / 2\n",
    "\n",
    "v_list = list(hash_list.items())\n",
    "v_list.sort(key=lambda x: x[1], reverse=True)\n",
    "mode = v_list[0][0]\n",
    "\n",
    "print(f'avg:{avg}, median:{med}, mode:{mode}({v_list[0][1]}), h_w_not_equal:{count}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50d3a7ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "interval = 3\n",
    "bin_num = (max(size_list)-min(size_list)) // interval\n",
    "bins = [i for i in range(min(size_list), max(size_list)+interval, interval)]\n",
    "fig = plt.figure(figsize=(30, 8), dpi=160)\n",
    "plt.hist(size_list, bins=bins)\n",
    "plt.xticks(ticks=bins)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c972c2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(max(size_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85b3986f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51d76ce7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def delete_wrong(ori_path, new_path):\n",
    "    ori_set = set(os.listdir(ori_path))\n",
    "    new_set = set([img for img in os.listdir(new_path) if len(img.split('_')) == 3])\n",
    "    print(len(ori_set), len(new_set))\n",
    "    print(((ori_set-new_set)))\n",
    "    #print('**********\\n**********\\n***********\\n*******\\n************\\n*************\\n***********\\n********')\n",
    "    print((len(new_set-ori_set)))\n",
    "    for img in new_set-ori_set:\n",
    "        shutil.move(os.path.join(new_path, img), '/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/wrong_basic_img/backup/val/0')\n",
    "    \n",
    "delete_wrong(f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/val/0/', f'/nfs3-p1/zsxm/dataset/aorta_classify_ct_{lower_b}_{upper_b}/wrong_basic_img/val/0/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c415746",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64c18d9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "backup_path = '/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/wrong_basic_img/backup'\n",
    "center_path = '/nfs3-p1/zsxm/dataset/aorta_classify_ct_-100_500/center'\n",
    "for dataset in ['train', 'val']:\n",
    "    for cat in range(4):\n",
    "        print(f'*****{dataset}/{cat}*****')\n",
    "        backup_set = set(os.listdir(os.path.join(backup_path, dataset, str(cat))))\n",
    "        for img in os.listdir(os.path.join(center_path, dataset, str(cat))):\n",
    "            if img in backup_set:\n",
    "                print(os.path.join(center_path, dataset, str(cat), img))\n",
    "                #os.remove(os.path.join(center_path, dataset, str(cat), img))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d30326aa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
